简体   繁体   中英

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. 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:

        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:
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.

Answer to my own question is that the url needs to be https not 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. Clearly, the webrequest needs the https hardcoded in the url.

Just tried to click the Accept button but that is not allowed for two days since I answered my own question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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