GetResponse keeps throwing page not found exception

208 Views Asked by At

Can you please help me out here? I am trying to get content of any web page. But GetResponse keeps throwing exception page not found. I appreciate your help. Following is my code.

try
{
    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.smallchiptechnologies.com/");
    request.Method = "POST";
    request.ContentType = "text/plain";               
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

}
catch (WebException ex)
{

}
2

There are 2 best solutions below

1
On

Have you discarded potential proxy issues? For example, if you are running that code behind a corporate webproxy, you'll need to modify slightly your code in order to support the connection thru that proxy.

Something like this...

webrequest.Proxy = new WebProxy(ProxyServer,ProxyPort);

More details here: https://msdn.microsoft.com/en-us/library/czdt10d3(v=vs.110).aspx

1
On

First, it looks like it should be GET response, not POST (since you just trying to get data from server and not posting any form data or something similar) so change request.Method = "POST"; to request.Method = "GET";

Second, you're not reading anything from response stream. Add something like this to your code to get page content:

string text;
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
    text =  reader.ReadToEnd();
}