4

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

我是这个概念的新手,因为我来自 C# 中的 Windows 窗体应用程序。

这是我加载的页面:

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

}

谢谢!

4

1 回答 1

7

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

Request.InputStream.Position = 0;
// now read from the stream
byte[] data = new byte[Request.InputStream.Length];
Request.InputStream.Read(data, 0, data.Length);
于 2012-12-13T15:11:35.723 回答