Link to home
Start Free TrialLog in
Avatar of StephenLiversidge
StephenLiversidge

asked on

get the http response stream in c#

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.

ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gautham Janardhan
Gautham Janardhan

str path is the url and assumed t5hat the respnse contains an xml u can cahnge it to ur requirement...
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
Avatar of StephenLiversidge

ASKER



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?
Thanks, everyone

All sorted now