-1

Hi i am trying to do a POST to a third party service and my post is not seeming to work correctly? my valuesxml has all my XML in it, also my url has a value set and i can see it when i watch it but it seems to not be posting my data properly. You see it connecting to the service but something must be wrong with my post method.

Here is my post

System.Net.HttpWebRequest request (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
string postdata = "DATA=" + valuexml.ToString();
byte[] postdatabytes = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(postdata);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postdatabytes.Length;
System.IO.Stream requeststream = request.GetRequestStream();
requeststream.Write(postdatabytes, 0, postdatabytes.Length);
requeststream.Close();

Just wanted to see if anyone can see if my code is wrong or anything which could be happening and causing this to do it.

Tim
  • 28,212
  • 8
  • 63
  • 76

3 Answers3

1

application/x-www-form-urlencoded requires that all keys and values be percent encoded. You are passing an XML blob, which will almost certainly contain characters that need encoding. This may be what is tripping up the service. Try this:

string postdata = "DATA=" + HttpUtility.UrlEncode(valuexml.ToString());

NOTE: There are various methods for URL encoding in .NET. This answer documents them all. Uri.EscapeDataString most closely matches the standard encoding rules for URL query parameters, but HttpUtility.UrlEncode most closely matches the standard encoding rules for POST data, hence my suggestion above.

Community
  • 1
  • 1
Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
0

We need details. For example, are you sure that the third party service accept posting data in this way? Looks bit strange if any service provider still accept this kind behavior, as it is rather difficult to control security.

If it is legitimate use, you can just set up a test server on your machine to see if the result meets your expectation first.

daxu
  • 3,514
  • 5
  • 38
  • 76
0

Instead of having to worry about the encoding, you can use .NET objects to do this for you. For example, FormUrlEncodedContent can take care of the encoding, and HttpClient can setup and do the post correctly. Using the .net functionality is not only less effort, but makes it less likely you're doing something non-standard or wrong that is causing the problem.

public async void SendData(string serviceUrl, string postData)
{
    // parameter(s) to post
    var formData = new List<KeyValuePair<string, string>>()
    {
        new KeyValuePair<string, string>("DATA", postData) 
    };

    // assemble the request content form encoded (reference System.Net.Http), and post to the url   
    var postTask = new HttpClient().PostAsync(serviceUrl, new FormUrlEncodedContent(formData));
    HttpResponseMessage responseMessage = await postTask;

    // if request was succesful, extract the response
    if (responseMessage.IsSuccessStatusCode)
    {
        using (StreamReader reader = new StreamReader(await responseMessage.Content.ReadAsStreamAsync()))
        {
            // now process the response
            string serviceResponse = reader.ReadToEnd();
        }
    }
}
steve16351
  • 5,372
  • 2
  • 16
  • 29