简体   繁体   English

为什么服务器看不到 HttpWebRequest 和方法 POST 数据

[英]Why HttpWebRequest and method POST data is not seen by the server

When I post to server using HttpWebRequest and method POST, the NameValueCollection in the asp code has no values.当我使用 HttpWebRequest 和方法 POST 发布到服务器时,asp 代码中的 NameValueCollection 没有值。 I have identical code working with other server pages, the only difference is the string data posted is a bit different.我有与其他服务器页面相同的代码,唯一的区别是发布的字符串数据有点不同。

code that posts is from a c# desktop application:发布的代码来自 c# 桌面应用程序:

        string responseFromServer = string.Empty;
        System.Net.HttpWebRequest request = null;
        System.IO.StreamReader reader = null;
        System.Net.HttpWebResponse response = null;
        string http = string.Empty;
        http = "http://www.apageonmywebsite.aspx";
            request = HttpWebRequest.Create(http) as HttpWebRequest;
            request.Method = "POST";
            UTF8Encoding encoding = new UTF8Encoding();
            //send a namevalue pair -that is what the website expects via the request object
            string postData = "TRIALID=" + System.Web.HttpUtility.UrlEncode(trialUserID, encoding);
            byte[] byte1 = encoding.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";  
            request.ContentLength = byte1.Length;
            request.Timeout = 20000;
            System.IO.Stream newStream = request.GetRequestStream();
            newStream.Write(byte1, 0, byte1.Length);
            newStream.Close();
            System.Threading.Thread.Sleep(1000);
            response = (HttpWebResponse)request.GetResponse();
            System.IO.Stream dataStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(dataStream);
            responseFromServer = reader.ReadToEnd();
            if (responseFromServer.Contains("\r"))
            {
                responseFromServer = responseFromServer.Substring(0, responseFromServer.IndexOf("\r"));
            }

Server code:服务器代码:

    NameValueCollection postedValues = Request.Form; // Request.Form worked locally, failed on server(count=0)
    IEnumerator myEnumerator = postedValues.GetEnumerator();
    try
    {
        foreach (string s in postedValues.AllKeys)
        {
            if (s == "TRIALID")
            {
                regcode += postedValues[s];
                break;
            }
        }
    }
    catch (Exception ex)
    {
        Response.Clear();
        Response.Write("FAILED");
        this.resultMsg = "FAILED. Exception: " + ex.Message;
        LogResult();
        return;
    }
    if (string.IsNullOrEmpty(regcode))
    {
        Response.Write("postedvalues count=" + postedValues.Count.ToString() + ": no regcode:");
        this.resultMsg ="postedvalues count=" + postedValues.Count.ToString() + ": no regcode:";
        LogResult();
        return;
    }

In the sending application, responseFromServer is postedvalues count=0:no regcode:在发送应用程序中,responseFromServer 是postedvalues count=0:no regcode:
So the data is posted but not "seen" on the server.所以数据被发布但在服务器上没有“看到”。
The trialUserID field used in the urlencode method is a string containing user domain name plus user name from the Environment object plus the machine name. urlencode 方法中使用的trialUserID字段是一个字符串,包含用户域名加上来自 Environment object 的用户名加上机器名。

Answer to my own question is that the url needs to be https not http.我自己的问题的答案是 url 需要是 https 而不是 http。 I converted my asp.net website to https one year ago and when I created the app that sends the posted data I assumed that since the entire website is configured to automatically redirect to https that should take care of it.一年前,我将我的 asp.net 网站转换为 https,当我创建发送发布数据的应用程序时,我假设由于整个网站配置为自动重定向到 Z5E056C500A1C4B6A7110B50D80BADE5Z5,所以应该小心。 Clearly, the webrequest needs the https hardcoded in the url.显然,webrequest 需要 url 中硬编码的 https。

Just tried to click the Accept button but that is not allowed for two days since I answered my own question.刚刚尝试单击“接受”按钮,但由于我回答了自己的问题,两天内不允许这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM