Home

Web requests?

I have input the following program:

string GetInput(string url)
{
HttpWebResponse response = null;
StreamReader readStream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url);
response = (HttpWebResponse)request.GetResponse ();

Console.WriteLine ("Content length is {0}", response.ContentLength);
Console.WriteLine ("Content type is {0}", response.ContentType);

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream ();
// Pipes the stream to a higher level stream reader with the required encoding format.
readStream = new StreamReader (receiveStream, Encoding.UTF8);

Console.WriteLine ("Response stream received.");
return readStream.ReadToEnd ();
}
catch(WebException wex)
{
Console.WriteLine (wex.ToString());
return null;
}
finally
{
if(response != null)
response.Close ();
if(readStream != null)
readStream.Close ();
}
}

void Main()
{
var input = GetInput("http://adventofcode.com/day/3/input");
input.Dump();
}

But I get an exception:

System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at UserQuery.GetInput(String url) in c:\TEMP\LINQPad\_zxalbbrp\query_kncder.cs:line 37

Am I missing something?

Thank you.

Comments

Sign In or Register to comment.