【发布时间】:2012-04-02 14:29:14
【问题描述】:
更新:我正在尝试将数据发布到 https URI。 POST 适用于 HTTP,但适用于 HTTPS uri
您好,我正在创建一个 c# winforms exe 来将数据发布到网站。代码如下。问题是,流复制了我的帖子数据..
例如:假设我想发布这个 -> username=bob
那我查流量的时候,实际发送的是,username=bobusername=bob
看到了吗?它复制,它再次将同一行添加到缓冲区的末尾并发送它。
两天后我想找到这个问题我快疯了。任何人都可以解决这个问题或给我一些提示吗?谢谢你。。
(内容长度正确设置为 12,但它发送 24 个字节,再次将相同的数据附加到缓冲区的尾部)
有标题
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();
我在byte[] postBytes = Encoding.ASCII.GetBytes(post_data); 和postBytes 上设置了一个断点,其中包含正确的数据...但它会输出两次。
为什么会这样?我希望我很清楚..
【问题讨论】: