Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

get the http response stream in c#

Posted on 2007-03-19
5
Medium Priority
?
779 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 2000 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 143

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

604 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