views:

113

answers:

1

Hello,

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 dont 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 im supposed to be receiving.

I cant set the contentlength because i dont 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();
A: 

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
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
Sounds like an encoding issue... I'll edit my answer to see if that helps
John
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
If all lines are in the 'bytes' array, then I can't think of anything else on your end that could be incorrect
John
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
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