Request.InputStream始终为空

时间:2012-12-13 15:07:49

标签: asp.net asp.net-mvc-3

我正在尝试发送请求的原始数据并进行一些调查,发现Page.Request.InputStream需要我需要,但当我进入我的页面时,它总是空的。

我是这个概念的新手,因为我来自C#的Windows Forms Application。

这是我加载的页面:

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);

}

谢谢!

1 个答案:

答案 0 :(得分:6)

也许某人已经阅读过InputStream并将其移至最后?在阅读之前尝试重置开头的位置:

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