locked
How to read a Http Request stream RRS feed

  • Question

  • User1909155429 posted

    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()
    
    Thursday, July 18, 2013 5:41 PM

All replies

  • User1983249378 posted

    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/

    Thursday, July 18, 2013 11:03 PM
  • User1909155429 posted

    It will not work for Request ?

    Thanks

    Friday, July 19, 2013 2:48 AM
  • User79986525 posted

    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 3:10 AM
  • User1909155429 posted

     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

    Friday, July 19, 2013 7:42 AM
  • User-1063667917 posted

    Did you find a way to preview the stream?

    Tuesday, December 31, 2013 5:20 AM
  • User1909155429 posted

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

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

    Tuesday, December 31, 2013 3:11 PM