XML HTTP

D

Dave

Hi,

I'm trying to post XMl to a URl but when I test it the text in the request stream doesn't appear to get sent:

// Configure secure certificate & credentials
req.ClientCertificates.Add(myCert);
req.Credentials = myCache;

// Setup HTTP Request headers
req.Method = "POST";
req.ProtocolVersion=HttpVersion.Version11;
req.ContentLength = postdata.Length;
req.ContentType = "multipart/form-data; boundary=[TGWLR]";
req.KeepAlive=false;
req.Headers.Add("Authorization",encCreds);
req.AllowWriteStreamBuffering=true;

// Create stream writer
Stream strWrite = req.GetRequestStream();
StreamWriter sw = new StreamWriter(strWrite);

// Set up post data
String postdata = "\n\n--[TGWLR]\n";
postdata += "Content-disposition: form-data; name=\"tgtest001\"; filename=\"tgtest001.xml\"\n";
postdata += "Content-type: text/xml\n\n";
postdata += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
postdata += XML;
postdata += "\n--[TGWLR]--\n\n";

// POST XML --- THIS DOES NOT APPEAR TO BE WORKING
sw.Write(postdata);
sw.Flush();
sw.Close();

Any clues anybody?

Also, if there's an easier/better way of doing this can you let me know?!

Cheers,
Dave Hart
System Developer
Telco Global Ltd.
 
J

Jon Skeet [C# MVP]

Dave said:
I'm trying to post XMl to a URl but when I test it the text in the request
stream doesn't appear to get sent:

// Configure secure certificate & credentials
req.ClientCertificates.Add(myCert);
req.Credentials = myCache;

// Setup HTTP Request headers
req.Method = "POST";
req.ProtocolVersion=HttpVersion.Version11;
req.ContentLength = postdata.Length;

This line is probably the mistake. In fact, I don't know what postdata
variable it's using, as you don't declare the local variable called
postdata until later on.
 
D

Dave

Ah, damn - that's me ripping the code out!

Sorry - in the actual code the postdata line is before all this code, I just
moved it into the wrong place when copying the code.

Any other ideas?!

Cheers,

Dave
 
J

Jon Skeet [C# MVP]

Dave said:
Ah, damn - that's me ripping the code out!

Sorry - in the actual code the postdata line is before all this code, I just
moved it into the wrong place when copying the code.

Might your XML data have any non-ASCII characters in? postdata.Length
is the length in characters, but the content length really has to be
the length in *bytes*. If you have any non-ASCII characters, they'll
end up taking more than one byte each, and thus your length will be
wrong.

(BOM, or byte-order markings, would affect this too, but I don't think
you'll end up with any.)

What does the request look like on the receiving side? (Run TcpTrace
from http://www.pocketsoap.com/tcptrace/ as a sort of proxy just to see
what the request headers etc are like.)
 
D

Dave

Ok, thanks for that... great tool!

The only problem is that I'm submitting a request to a secure site which is
encrypted in the tcptrace. I created a wee app just to receive a non-secure
post and it works fine, but for some reason the secure post (which is
necessary) dies horribly.

If the data is succeeding, I guess that would indicate that the data's ok
and it's something wrong with the security? Is there any extra with
security I need to do?

Thanks,
Dave
 
J

Jon Skeet [C# MVP]

Dave said:
Ok, thanks for that... great tool!

The only problem is that I'm submitting a request to a secure site which is
encrypted in the tcptrace. I created a wee app just to receive a non-secure
post and it works fine, but for some reason the secure post (which is
necessary) dies horribly.

Ah. Rats.
If the data is succeeding, I guess that would indicate that the data's ok
and it's something wrong with the security? Is there any extra with
security I need to do?

Well, what exactly are you seeing? How much control have you got over
what's going on at the other end?
 
D

Dave

No control unfortunately, I'm sending to a external gateway.

However, I removed an extra line between the header and the body and now am
getting a Server error. Do you think this is due issues with the target
server rather than my posting?

Thanks,
Dave
 
J

Jon Skeet [C# MVP]

Dave said:
No control unfortunately, I'm sending to a external gateway.

However, I removed an extra line between the header and the body and now am
getting a Server error. Do you think this is due issues with the target
server rather than my posting?

Hard to say. What exactly do you mean by "an extra line"? A line that
definitely shouldn't be there, or just one that looks slightly dodgy?
If you use http instead of https without the extra line, what happens?

What error are you getting over https anyway? Is any of the request
stream being used?
 
D

Dave

Removed an extra empty CRLF from the body...wasn't dodgy just meant there
were two as the header automatically puts a CRLF in after writing.

Getting 500 Server Error now... Going to raise it with the gateway
providers... I'll let you know...!

Cheers,
Dave
 
D

Dave

Hi,

In my HTTP headers I have "Expect: 100-continue". Is there any way I can
prevent this from appearing?

Thanks,
Dave

Dave said:
Removed an extra empty CRLF from the body...wasn't dodgy just meant there
were two as the header automatically puts a CRLF in after writing.

Getting 500 Server Error now... Going to raise it with the gateway
providers... I'll let you know...!

Cheers,
Dave

now
 
J

Jon Skeet [C# MVP]

Dave said:
In my HTTP headers I have "Expect: 100-continue". Is there any way I can
prevent this from appearing?

Hmm... not that I know of. That could certainly be causing problems, if
it hasn't been implemented properly on the server side.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top