【发布时间】:2012-01-16 04:45:26
【问题描述】:
我需要使用 C# 进行 HTTP POST。它需要以与 IE6 页面相同的方式进行回发。
从文档来看,回发应该是这样的
POST /.../Upload.asp?b_customerId=[O/M1234] HTTP/1.1
Content-length: 12345
Content-type: multipart/form-data; boundary=vxvxv
Host: www.foo.com
--vxvxv
Content-disposition: form-data; name=”File1”; filename=”noColonsSpacesOrAmpersandsInHere”
Content-type: text/xml
<?xml version=”1.0” encoding=”UTF-8”?>
...
<bat:Batch ...
.......
</bat:Batch>
--vxvxv--
我认为边界字符有问题。我尝试在发布数据中设置边界,提琴手显示类似的内容,但我得到一个页面,并显示错误“无效的过程调用或参数”。 Content-disposition 位于正文而不是标题中,以将其保持在边界内。我不确定这是对的。我是否以正确的方式设置边界? 谁能提供一些关于如何使用 C# 进行 IE6 样式 HTTP POST 的指导?谢谢
我的代码
data = "--vxvxv" + Environment.NewLine +
"Content-disposition: form-data; name=\"File1\";" + Environment.NewLine +
"filename=\"provideTest.xml\"" + Environment.NewLine +
"Content-type: text/xml" + Environment.NewLine +
@"<?xml version=""1.0"" encoding=""UTF-8""?>" + Environment.NewLine +
data + Environment.NewLine +
"--vxvxv--";
var encoding = ASCIIEncoding.UTF8;
HttpWebRequest request;
var postData = encoding.GetBytes(data);
request = (HttpWebRequest)WebRequest.Create(url);
request.ContentLength = postData.Length;
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=vxvxv";
request.Host = "www.foo.com";
request.ContentLength = postData.Length;
X509Certificate2Collection certCollect = new X509Certificate2Collection();
X509Certificate2 cert = new X509Certificate2(@"C:\a\cert.pfx", "password");
certCollect.Add(cert);
request.ClientCertificates = certCollect;
using (Stream writeStream = request.GetRequestStream()) {
writeStream.Write(postData, 0, postData.Length); }
WebResponse webResponse = request.GetResponse();
string output = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
LogEntry.Write("Recieved : " + output);
return output;
提琴手输出(原始)
POST https://../Upload.asp?b_customerId=%5BO/M1234%5D HTTP/1.1
Content-Type: multipart/form-data; boundary=vxvxv
Host: www.foo.com
Content-Length: 5500
Expect: 100-continue
Connection: Keep-Alive
--vxvxv
Content-disposition: form-data; name="File1";
filename="provideTest.xml"
Content-type: text/xml
<?xml version="1.0" encoding="UTF-8"?>
...SNIP...
</bat:Batch>
--vxvxv--
【问题讨论】:
-
首先想到的是你的文件名在一个不应该的新行上。不知道这是否会导致提到的错误,但至少它可能会让事情变得很混乱。
-
很确定这会使它无效。
-
用换行符试过了。结果相同
-
您在
@"<?xml version=""1.0"" encoding=""UTF-8""?>" + Environment.NewLine + data +中使用了相同的变量,这是正确的还是这里只是一个错字
标签: c# internet-explorer