0

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)
{

}
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Tom
  • 61
  • 3
  • 9
  • 1
    Why are you using `POST` to get the content of a web page? `POST` is used to send data to the server, like form inputs. To just get the content of a page you have to use `GET` – Thomas Lielacher Apr 08 '15 at 14:42
  • Thanks Thomas for your reply. I changed POST to GET but no luck. I also did what Ulugbek suggested like adding contentLength. Do you think this could be network issue? – Tom Apr 08 '15 at 14:50
  • What is the exact exception you are getting? – Thomas Lielacher Apr 08 '15 at 14:52
  • System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse() at Translate.TranslateText.Program.Main(String[] args) – Tom Apr 08 '15 at 14:54
  • @Tom have you provided actual page address you're trying to get data from? It works perfectly from my computer both in browser and from c# code without getting 404. Are you able to open that page you're trying to access in browser? – Andrey Korneyev Apr 08 '15 at 14:55
  • Thanks Andy for your verification. So this confirms that there is something wrong in my setup. Do you guys think i got to set something in my firewall or anything anywhere else. I appreciate your time. – Tom Apr 08 '15 at 14:59
  • @Andy i am able to get that page in web browser. – Tom Apr 08 '15 at 15:03
  • @Tom are you behind some proxy? If yes - try to supply its parameters using `request.Proxy` property. – Andrey Korneyev Apr 08 '15 at 15:06
  • I just checked internet connection settings. I dont see any proxy. But let me tell you that our internet do gets routed through Germany since i work for a German company. – Tom Apr 08 '15 at 15:10
  • Andy, Is there anything i can try. Is there any way to get proxy server info if there is any on the way to ISP. – Tom Apr 08 '15 at 16:35

2 Answers2

0

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();
}
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • Thanks Andy, Actually i did not add any further processing to response because its always null. I thought of fixing that issue first then do it – Tom Apr 08 '15 at 14:52
0

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