Post Request / Rest Request - Page doesnt receive any data

Antonio Developer asked on July 5, 2017 13:51

Hello everybody

Iam trying to sent a POST Request to a specific aspx Page but my Page does not receive any data. Request.Form in aspx Page is empty.. What Iam missing?

Pageload() in customPage.aspx.cs

int orderId = ValidationHelper.GetInteger(Request.Form["orderId"], 0);

SendRequest method

private void sendRESTRequest(string orderId)
{
    string httpMethod = "POST"; 
    string requestUrl = "http://localhost:8080/CMSModules/Ecommerce/CMSPages/customPage.aspx"; 
    string requestData; // Data for POST or PUT requests

        var values = new Dictionary<string, string>
        {
           { "orderId", orderId },
        };

        requestData = values.ToString();
        string responseDescription; // Stores the description of the response status
        string responseData; // Stores data retrieved by the request

        // Creates the REST request
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
        // Sets the HTTP method of the request
        request.Method = httpMethod;
        // Authorizes the request using Basic authentication
        request.Headers.Add("Authorization: Basic YWRtaW5pc3RyYXRvcjo=");

        LogEvent("Post Request", "EST");

        // Submits data for POST or PUT requests
        if (request.Method == "POST" || request.Method == "PUT")
        {
            request.ContentType = "text/xml"; 
            Byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(requestData);
            request.ContentLength = bytes.Length;

            using (Stream writeStream = request.GetRequestStream())
            {
                writeStream.Write(bytes, 0, bytes.Length);
            }
        }
        // Gets the REST response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        // Stores the description of the response status
        responseDescription = (Int32)response.StatusCode + " - " + response.StatusDescription;

        // Gets the response data
        using (Stream responseStream = response.GetResponseStream())
        {
            if (responseStream != null)
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    responseData = HttpUtility.HtmlEncode(reader.ReadToEnd());
                }
        }
}

Recent Answers


Trevor Fayas answered on July 5, 2017 15:36

First i would see if your values.ToString() is actually giving a proper value, my guess is firstly values.ToString() on a dictionary object will just return the class name (Dictionary<string, string>).

I would try a NameValueCollection object instead.

https://stackoverflow.com/questions/14702902/post-form-data-using-httpwebrequest

Can you verify the text of the post data and put it here?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.