Share via


How to read a Http Request stream

Question

Thursday, July 18, 2013 5:41 PM

I need to obtain Request and Response data as a text string format.

Below is code that converts the data parameters to byte then adds further data to a stream.

I want to read the whole Request as a string of characters.

 

Dim paypalParamBytes As Byte() = Encoding.ASCII.GetBytes(Parameters)

Dim request As WebRequest = WebRequest.Create(siteAddress)
' add header info here
  writer = request.GetRequestStream()

        writer.Write(paypalParamBytes, 0, paypalParamBytes.Length)

        writer.Close()

All replies (6)

Thursday, July 18, 2013 11:03 PM

Using this you can read httprequest stream

StreamReader reader = new StreamReader(resp.GetResponseStream()); string result = reader.ReadToEnd(); 

For full code 

http://umairaslam.com/index.php/call-api-from-c-shar/


Friday, July 19, 2013 2:48 AM

It will not work for Request ?

Thanks


Friday, July 19, 2013 3:10 AM

Hi ,

Try somthing like this 

protected void Button1_Click(object sender, EventArgs e)
    {
        string xml = "&lt;?xml version='1.0' encoding='UTF-8'?><request><header><userId>*GUEST</userId><password>guest</password></header><body><command>searchJobs</command><parms><parm>100</parm><parm>Open</parm><parm></parm><parm></parm><parm></parm><parm></parm><parm></parm><parm></parm></parms></body></request>";
        string url = "http://ww1.caliber.ie/cram/api";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);


        //string s = "id="+Server.UrlEncode(xml);
        byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xml);
        req.Method = "POST";
        req.ContentType = "text/xml;charset=utf-8";
        req.ContentLength = requestBytes.Length;
        Stream requestStream = req.GetRequestStream();
        requestStream.Write(requestBytes, 0, requestBytes.Length);
        requestStream.Close();


        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
        string backstr = sr.ReadToEnd();
        TextBox1.Text = backstr;
        sr.Close();
        res.Close();
    }

or check the link 

http://forums.asp.net/t/1308407.aspx/1


Friday, July 19, 2013 7:42 AM

 Stream requestStream = req.GetRequestStream();
        requestStream.Write(requestBytes, 0, requestBytes.Length);

But how can the stream of bytes be converted back to string for preview ?

i need to see the whole string of data.

Thanks for your kind help


Tuesday, December 31, 2013 5:20 AM

Did you find a way to preview the stream?


Tuesday, December 31, 2013 3:11 PM

i believe i did though it was that long ago i forget.

i may have viewed the string data before putting on the stream ?