简体   繁体   中英

HTTPS POST in C#, Winforms (Stream Writer, HttpWebResponse, HttpWebRequest)

UPDATE: I am trying to POST data to https URI. The POST works for HTTP but it fails for HTTPS uri


Hi I am creating ac# winforms exe to post data to a website. The code is below. The issue is, the stream duplicates my post data..

eg: suppose I want to post this -> username=bob

Then when I check the traffic, what is actually sent is, username=bobusername=bob

See? It duplicates, it adds the same line once more to the end of the buffer and sends it.

I am going crazy trying to find the issue from two days.. Can any body solve this or give me some hints please? thank you..

(content length is correctly set to 12, but it sends 24 bytes, after appending same data once again to the tail of buffer)

There are the headers

POST /login/ HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Host: abc.test.com
Content-Length: 12

username=bobusername=bob
-

This is the code I am currently using

string post_data = "username=bob";
string uri = "https://abc.test.com/login/";

HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(uri); 
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";

byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;

Stream requestStream = request.GetRequestStream();

requestStream.Write(postBytes, 0, postBytes.Length);

MessageBox.Show(postBytes.Length.ToString());
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string tmp = sr.ReadToEnd().Trim();

I put a breakpoint on line byte[] postBytes = Encoding.ASCII.GetBytes(post_data); and postBytes contains the correct data... but it gets output twice.

Why is this happening? I hope I am clear..

I tried out your code and it seemed to work as expected (sent a HTTP Post with a 12byte payload) after I changed the host in the URI to something that was addressable (used http://adsf.com/login ). Here's the trace from wireshark:

Wireshark跟踪

You might try out the URI I used to see what you get, this will at least rule out your computer or code as possible sources of the problem. If the problem disappears when using a different URI then the problem might be between your network equipment and the web server (reverse-proxy configuration, webserver configuration, network switch configuration, etc).

You can try to get more information by setting trace configuration as described in this page . When I tried your code, I get the following output:

System.Net Verbose: 0 : [2324] Data from ConnectStream#26756241::Write
System.Net Verbose: 0 : [2324] 00000000 : 75 73 65 72 6E 61 6D 65-3D 62 6F 62             : username=bob
System.Net Verbose: 0 : [2324] Exiting ConnectStream#26756241::Write()

Looks like data is correctly written to the ConnectStream. Something wrong somewhere else?

And don't forget to close the WebResponse object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM