Use WebProxy : WebProxy « Network « ASP.Net






Use WebProxy

<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">

private void SubmitButton_Click(System.Object sender, System.EventArgs e){
  String RequestUrl = "http://www.cnn.com/";

  HttpWebRequest WebRequestObject = null;
  StreamReader sr = null;
  HttpWebResponse WebResponseObject = null;
  
  try{

    WebRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
    WebRequestObject.Method = "GET";
    
    WebRequestObject.Proxy = new WebProxy("http://myproxy:80/"); // fails unless a real proxy is specified

    WebResponseObject = (HttpWebResponse) WebRequestObject.GetResponse();
    sr = new StreamReader(WebResponseObject.GetResponseStream());
    
    String Results = sr.ReadToEnd();
    WebResponseLabel.Text = Results;
    WebResponseText.Text = Results;
  }
  finally{
    try{
      sr.Close();
    }
    catch{}
    
    try{
      WebResponseObject.Close();
      WebRequestObject.Abort();
    }
    catch{}
  }
}

</script>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <form runat="server" ID="Form1">
      <asp:Button Runat="server" ID="SubmitButton" Text="Get Page Requiring Proxy" OnClick="SubmitButton_Click" /><br>
      <asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" /><br>
      <asp:Label Runat="server" ID="WebResponseLabel" />
    </form>
  </body>
</html>

 








Related examples in the same category