XandrUu
XandrUu

Reputation: 1179

Request.InputStream always empty

I'm trying to send a raw data of a request and done some research and found out that the Page.Request.InputStream has want I need but when I enter my page it's always empty.

I'm new at this concept because I'm coming from Windows Forms Application in C#.

This is my page that I load:

public override void process(NameValueCollection parametersReceived)
    {
        string urlDestination = "";
        urlDestination = parametersReceived[Includes.SUPPLIER_URL];

        Stream str; String strSoapMessage = "";
        Int32 strLen, strRead;

        str = Request.InputStream;

        strLen = Convert.ToInt32(str.Length);

        byte[] strArr = new byte[strLen];

        strRead = str.Read(strArr, 0, strLen);

        for (int i = 0; i < strLen; i++)
        {
            if (strSoapMessage.Length > 0)
                strSoapMessage += "&";
            strSoapMessage += strArr[i].ToString();
        }

        saveMyLog("DATA COMMUNICATION: \n Destination: " + urlDestination + "\n Request: " + strSoapMessage);


        HttpWebRequest webRequest = null;

        webRequest = (HttpWebRequest)WebRequest.Create(new Uri(urlDestination));
        webRequest.Method = "POST";
        webRequest.ContentType = "text/xml";
        webRequest.Headers.Add("SOAPAction", urlDestination);

        using (StreamWriter requestStream = new StreamWriter(webRequest.GetRequestStream()))
        {
            requestStream.Write(strSoapMessage);
        }


        strSoapMessage = "";
        strSoapMessage = new StreamReader(webRequest.GetResponse().GetResponseStream()).ReadToEnd();

        Response.Write(strSoapMessage);
        saveMyLog("\n Status: " + ((HttpWebResponse)webRequest.GetResponse()).StatusCode.ToString() + "\n Response: " + strSoapMessage);

}

Thanks!

Upvotes: 4

Views: 4972

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Maybe someone already read the InputStream and moved it to the end? Try resetting the position at the beginning before reading it:

Request.InputStream.Position = 0;
// now read from the stream
byte[] data = new byte[Request.InputStream.Length];
Request.InputStream.Read(data, 0, data.Length);

Upvotes: 7

Related Questions