Solved

get the http response stream in c#

Posted on 2007-03-19
5
754 Views
Last Modified: 2011-10-03
Hi all,

heres what ive been asked to do, I need to read the http responses of a website into a database, im working in c# and not to sure how to go about this one.

0
Comment
Question by:StephenLiversidge
  • 2
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 18747225
WebRequest req = null;
                  WebResponse rsp = null;
Stream rspStream = null,reqStream = null;
req = WebRequest.Create(strPath);
rsp = req.GetResponse();
                                    rspStream = rsp.GetResponseStream();      
if (rspStream != null)
                        {
                              // Load the XmlTextReader from the stream                                                      
                              XmlTextReader xmlread = null;
                              xmlread = new XmlTextReader (rspStream);

                              //SAVE THE RESPONSE XML assumed
                              XmlDocument doc = new XmlDocument();
                              doc.Load(xmlread);
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18747229
str path is the url and assumed t5hat the respnse contains an xml u can cahnge it to ur requirement...
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18747268
here is a class I use in my project(s):

#region HTTP Request
      public class HTTPRequest
      {
            private string serverIp = "";
            private int serverPort = 0;
            private string serverUsername = "";
            private string serverPassword = "";
            
            public HTTPRequest(string ip,int port,string username,string password) {
                  serverIp = ip;
                  serverPort = port;
                  serverUsername = username;
                  serverPassword = password;
            }

            public string Submit(string servlet, string param)
            {                  
                  return Submit("/"+servlet+"?"+param);
            }

            public string Submit(string url)
            {
                  int tries = 0;
                  string s = "";

                              TcpClient tcp = new TcpClient();
                              tcp.Connect(serverIp,serverPort);
                              tcp.ReceiveTimeout = 20000;
                              s = SubmitTcp(tcp,Header(url));
                              tcp.Close();
                  return s;
            }

            private string Header(string url) {
                  string header = "";
                  UTF8Encoding Uuserpass = new UTF8Encoding();
                  string userpass = Convert.ToBase64String(Uuserpass.GetBytes(serverUsername+":"+serverPassword));
                  header = "GET "+url+" HTTP/1.1\r\nAuthorization: Basic "+userpass+"\r\n\r\n";
                  header += "Content-type: application/x-www-form-urlencoded\r\n";
                  header += "Content Length: "+url.Length+"\r\n";
                  header += "Connection: close\r\n\r\n";
                  return header;
            }

            private string SubmitTcp(TcpClient tcp,string header) {
                  string response = "";

                  ASCIIEncoding encode = new ASCIIEncoding();
                  byte[] b = encode.GetBytes(header);
                  Stream stream = tcp.GetStream();
                  stream.Write(b,0,b.Length);

                  byte[] ret = new byte[1000];
                  int k = 0;
                  while ((k=stream.Read(ret,0,1000)) > 0) {
                        for (int i=0;i<k;i++) response += (Convert.ToChar(ret[i]));
                  }
                  stream.Close();
                  return response;
            }
      }
      #endregion
0
 

Author Comment

by:StephenLiversidge
ID: 18747445


Hi, Im getting this message at the min, very frustrating

The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

The url im trying to grab is www2.url.com though, would this make a difference?
0
 

Author Comment

by:StephenLiversidge
ID: 18755725
Thanks, everyone

All sorted now
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question