but still i am unable to get the http response ... i am using code below , this code works fine for simpler static sites , but i dnt knw why it is not working for the urls mentioned above
my code
string url = "http://www.pandemonium.in/1/posts/1/8/12792.html";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
Console.WriteLine("\r\nResponse stream received.");
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
Console.WriteLine("HTML...\r\n");
string response = "";
if(!File.Exists(@"C:\Users\ASAD\Documents\Visual Studio 2010\Projects\httpResponse\httpResponse\bin\Debug\response.txt"))
{
File.Create(@"C:\Users\ASAD\Documents\Visual Studio 2010\Projects\httpResponse\httpResponse\bin\Debug\response.txt");
}
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
String str = new String(read, 0, count);
response += str;
//Console.Write(str);
count = readStream.Read(read, 0, 256);
}
StreamWriter sr = new StreamWriter(@"C:\Users\ASAD\Documents\Visual Studio 2010\Projects\httpResponse\httpResponse\bin\Debug\response.txt");
sr.Write(response);
sr.Close();
Console.WriteLine("");
if(response.Contains("DIY Costume Closet (SOJ01106)"))
{
Console.WriteLine("OK");
}
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();
Console.Read();
try # 1: 2012-12-29 15:02:21
try # 2: 2012-12-29 15:02:22
ConnectStream
System.Net.ConnectStream
CanTimeout True
ReadTimeout 300000
WriteTimeout 300000
CanRead True
CanSeek False
CanWrite False
Length NotSupportedException
This stream does not support seek operations.
Message This stream does not support seek operations.
et cetera
note:
Length NotSupportedException
This stream does not support seek operations.
Message This stream does not support seek operations.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
try
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// add user-agent header to http request , and all works fine
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
string response = "";
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
String str = new String(read, 0, count);
response += str;
//Console.Write(str);
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();
}
else
{
return "";
}
}
catch
{
}
Marked as answer by asad.aries on Dec 31, 2012 05:47 PM
asad.aries
Member
86 Points
37 Posts
Http web request and response
Dec 28, 2012 01:50 PM|LINK
Hye i have been tring to get an http web response or basic html of url like these
http://www.adamant.in/1/posts/1/8/19857.html
http://www.batalion.in/1/posts/1/8/24572.html
http://www.xenofob.in/1/posts/1/8/9377.html
http://www.bastillads.in/1/posts/1/8/36351.html
http://www.panchmahalbusinessworld.co.in/1/posts/1/8/84458.html
http://www.panchmahalbusinessworld.in/1/posts/1/8/84425.html
but still i am unable to get the http response ... i am using code below , this code works fine for simpler static sites , but i dnt knw why it is not working for the urls mentioned above
my code
string url = "http://www.pandemonium.in/1/posts/1/8/12792.html"; HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // Sends the HttpWebRequest and waits for the response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); // Gets the stream associated with the response. Stream receiveStream = myHttpWebResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); // Pipes the stream to a higher level stream reader with the required encoding format. StreamReader readStream = new StreamReader(receiveStream, encode); Console.WriteLine("\r\nResponse stream received."); Char[] read = new Char[256]; // Reads 256 characters at a time. int count = readStream.Read(read, 0, 256); Console.WriteLine("HTML...\r\n"); string response = ""; if(!File.Exists(@"C:\Users\ASAD\Documents\Visual Studio 2010\Projects\httpResponse\httpResponse\bin\Debug\response.txt")) { File.Create(@"C:\Users\ASAD\Documents\Visual Studio 2010\Projects\httpResponse\httpResponse\bin\Debug\response.txt"); } while (count > 0) { // Dumps the 256 characters on a string and displays the string to the console. String str = new String(read, 0, count); response += str; //Console.Write(str); count = readStream.Read(read, 0, 256); } StreamWriter sr = new StreamWriter(@"C:\Users\ASAD\Documents\Visual Studio 2010\Projects\httpResponse\httpResponse\bin\Debug\response.txt"); sr.Write(response); sr.Close(); Console.WriteLine(""); if(response.Contains("DIY Costume Closet (SOJ01106)")) { Console.WriteLine("OK"); } // Releases the resources of the response. myHttpWebResponse.Close(); // Releases the resources of the Stream. readStream.Close(); Console.Read();gerrylowry
All-Star
20513 Points
5712 Posts
Re: Http web request and response
Dec 29, 2012 07:13 PM|LINK
@ asad.aries
you appear to have marked your issue as resolved, however, you have not told us how you resolved your issue.
http://www.pandemonium.in/1/posts/1/8/12792.html is a valid URL, however, it appears that an exception is occurring here:
adding some try/catch code:
// valid: http://www.pandemonium.in/1/posts/1/8/12792.html string url = "http://www.pandemonium.in/1/posts/1/8/12792.html"; System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); // Sends the HttpWebRequest and waits for the response. try { Console.WriteLine ("try # 1: {0}", DateTime.Now); System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse(); // Gets the stream associated with the response. try { Console.WriteLine ("try # 2: {0}", DateTime.Now); Stream receiveStream = myHttpWebResponse.GetResponseStream(); Console.WriteLine (receiveStream); } catch (System.NotSupportedException systemNetConnectStream) { Console.WriteLine ("catch # 2a: {0}", DateTime.Now); Console.WriteLine (systemNetConnectStream.Message); throw; } catch (Exception ex) { Console.WriteLine ("catch # 2b: {0}", DateTime.Now); Console.WriteLine (ex.Message); throw; } } catch (Exception aaaaa) { Console.WriteLine ("catch # 1: {0}", DateTime.Now); Console.WriteLine ("Error: {0}", aaaaa.Message); throw; }i get this output:
try # 1: 2012-12-29 15:02:21 try # 2: 2012-12-29 15:02:22 ConnectStream System.Net.ConnectStream CanTimeout True ReadTimeout 300000 WriteTimeout 300000 CanRead True CanSeek False CanWrite False Length NotSupportedException This stream does not support seek operations. Message This stream does not support seek operations. et ceteranote:
Length NotSupportedException
This stream does not support seek operations.
Message This stream does not support seek operations.
g.
asad.aries
Member
86 Points
37 Posts
Re: Http web request and response
Dec 31, 2012 05:37 PM|LINK
just by simply adding headers to request
as
try { HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // add user-agent header to http request , and all works fine myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"; // Sends the HttpWebRequest and waits for the response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); // Gets the stream associated with the response. if (myHttpWebResponse.StatusCode == HttpStatusCode.OK) { Stream receiveStream = myHttpWebResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); // Pipes the stream to a higher level stream reader with the required encoding format. StreamReader readStream = new StreamReader(receiveStream, encode); string response = ""; Char[] read = new Char[256]; // Reads 256 characters at a time. int count = readStream.Read(read, 0, 256); while (count > 0) { // Dumps the 256 characters on a string and displays the string to the console. String str = new String(read, 0, count); response += str; //Console.Write(str); count = readStream.Read(read, 0, 256); } // Releases the resources of the response. myHttpWebResponse.Close(); // Releases the resources of the Stream. readStream.Close(); } else { return ""; } } catch { }