简体   繁体   中英

HTTP POST request issue

I am sending a HTTP POST request to PHP server using HttpWebRequest . My code works like this:

    /// <summary>
    /// Create Post request.
    /// </summary>
    /// <param name="requestUrl">Requested url.</param>
    /// <param name="postData">Post data.</param>
    /// <exception cref="RestClientException">RestClientException</exception>
    /// <returns>Response message, can be null.</returns>
    public string Post(string requestUrl, string postData)
    {
        try
        {
            //postData ends with &
            postData = "username=XXX&password=YYYY&" + postData;

            byte[] data = UTF8Encoding.UTF8.GetBytes(postData);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
            request.Method = RequestMethod.Post.ToString().ToUpper();
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
                stream.Flush();
            }

            string responseString;
            using (WebResponse response = request.GetResponse())
            {
                responseString = GetResponseString(response);
            }

            return responseString;
        }
        catch (WebException ex)
        {
            var httpResponse = (HttpWebResponse)ex.Response;
            if (httpResponse != null)
            {
                throw new RestClientException(GetExceptionMessage(httpResponse));
            }

            throw;
        }
    }

I am experiencing wierd behaviour. Every minute I am sending like 100 requests using this. But from time to time, this request is executed withou POST data. Then my PHP server returns error (because I am checking if request is POST and if it has any POST data).

This is a client/server communication. Computer with Windows Service application is connected to the internet using Wifi. Connection is sometimes really poor. Can this cause issue mentioned? How to make HTTP POST request secure against this behaviour.

I don't see any "customized" behaviour you use, so why don't you use the WebClient.UploadData method? then you will know it is not something you are doing wrong

it does all of the "dirty" work for you, and you can add the content-type header as well.

look at that link for more info: http://msdn.microsoft.com/en-us/library/tdbbwh0a(v=vs.80).aspx

example:

public string Post(string requestUrl, string postData)
{
   WebClient myWebClient = new WebClient();
   myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
   byte[] data = UTF8Encoding.UTF8.GetBytes(postData);
   byte[] responseArray = myWebClient.UploadData(requestUrl,data);
   return responseArray;
}

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