2

Im stuck on this httpWebRequest problem. I need to send XML to a website. But I keep getting negative responses on my request. I saw some code examples where the ContentLength was set... And that might be the issue but I don't know....

The XML written in writePaymentRequest(...) is exactly as the website needs it to be, because they got my xml markup and they succeeded, in another programming language though. The result only contains their error instead of the information I'm supposed to be receiving.

I can't set the contentlength because I don't know the length when I create the writer with the requeststream in it.

HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://some.website.com");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";

using (writer = new XmlTextWriter(httpWebRequest.GetRequestStream(), System.Text.Encoding.UTF8))
{
 writePaymentRequest(writer, registrant, amount, signature, ipaddress);
}

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
String stringResult = streamReader.ReadToEnd();
streamReader.Close();
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rickjaah
  • 581
  • 1
  • 6
  • 16

1 Answers1

4

You would know the length if you wrote the XmlTextWriter to something like a MemoryStream first. From there, you could get the bytes, set the httpWebRequest.ContentLength to the length of the byte array, and then write the byte array to your request

edit

The middle of your code would look something like this (I think):

    MemoryStream ms = new MemoryStream();
    using (writer = new XmlTextWriter(ms, System.Text.Encoding.UTF8))
    {
        writePaymentRequest(writer, registrant, amount, signature, ipaddress);
    }
    byte[] bytes = ms.ToArray();
    ms.Close();
    httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length);
    httpWebRequest.ContentLength = bytes.Length;

edit #2

Instead of XmlTextWriter(ms, System.Text.Encoding.UTF8), try XmlTextWriter(ms, new UTF8Encoding(false)) to see if that fixes the encoding issue

John
  • 17,163
  • 16
  • 65
  • 83
  • This doesnt work. Unfortunately it gives the same error. I do see three question marks in the bytearray(converted to string) on the start of the request. could this be something? It's probably a simple fix... The connection works, because it gives an XML based error message. – Rickjaah Feb 01 '10 at 14:40
  • Sounds like an encoding issue... I'll edit my answer to see if that helps – John Feb 01 '10 at 14:48
  • I just had a word with the website... It says it only receives the first line.... so somewhere something is going wrong in pushing the request... – Rickjaah Feb 01 '10 at 14:53
  • If all lines are in the 'bytes' array, then I can't think of anything else on your end that could be incorrect – John Feb 01 '10 at 15:02
  • John, thank you. The 'different' UTF-8 encoding (new UTF8Encoding()) did the trick! Why didnt it work with the other one? Do you know that? – Rickjaah Feb 01 '10 at 15:29
  • 2
    by default, the encoding will emit a 3-byte descriptor letting us know that it's utf8encoding (the 3 question marks you saw). Initializing it with 'false' tells it not to write those out – John Feb 01 '10 at 15:32