Solved

get the http response stream in c#

Posted on 2007-03-19
5
749 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
Comment Utility
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
Comment Utility
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]
Comment Utility
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
Comment Utility


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
Comment Utility
Thanks, everyone

All sorted now
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now