HTTPS POST在C#中的WinForms(流作家,HttpWebResponse,HttpWebRequest的) [英] HTTPS POST in C#, Winforms (Stream Writer, HttpWebResponse, HttpWebRequest)

查看:220
本文介绍了HTTPS POST在C#中的WinForms(流作家,HttpWebResponse,HttpWebRequest的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我想POST数据到https URI。该POST适用于HTTP,但它不能用于HTTPS URI

我创建一个C#的WinForms exe文件来POST数据到网站。的代码如下。 > =用户名鲍勃

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

如$ C>

然后当我检查交通,有什么实际发送是,用户名= bobusername =鲍勃

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..

(内容长度正确设置为12,但它再次发送24字节,追加相同的数据后,缓冲区的尾部)

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

有头

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

username=bobusername=bob
-

这是我目前使用的

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();



我把行字节[] = postBytes一个Encoding.ASCII断点。 GetBytes会(post_data); postBytes 包含正确的数据...但它可以输出两次

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..

推荐答案

我尝试了你的代码,它似乎按预期工作(发送HTTP POST与一个12byte的有效载荷)后,我改变了主机的URI的东西,是可寻址(使用 http://adsf.com/login )。下面是Wireshark的跟踪:

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:

您可以尝试我曾经看到你,这至少可以排除您的计算机或代码作为问题的可能来源是什么URI。如果问题采用不同的URI时消失,那么问题可能是您的网络设备和Web服务器(反向代理配置,Web服务器配置,网络交换机配置等)。

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).

这篇关于HTTPS POST在C#中的WinForms(流作家,HttpWebResponse,HttpWebRequest的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆