我尝试使用C#重复浏览器发布请求但是到目前为止我的C#代码没有给出预期的结果。
正确的代码应该是什么?
Firebug关于请求的一些信息。
的接头:
Host:"test-url:8080"
User-Agent:"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"
Accept:"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
Accept-Language:"ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3"
Accept-Encoding:"gzip, deflate"
Referer:"http://test-url:8080/Emul/report.jsp?type=1"
Cookie:"JSESSIONID=31DF4BFA5ECA4014EE091774D0719AC0"
Connection:"keep-alive"
Upgrade-Insecure-Requests:"1"
PARAMS :
Content-Type: multipart/form-data; boundary=---------------------------1987383936223
Content-Length: 5641
-----------------------------1987383936223
Content-Disposition: form-data; name="type"
1
-----------------------------1987383936223
Content-Disposition: form-data; name="file"; filename="409474.xml"
Content-Type: text/xml
<?xml version="1.0" encoding="utf-8"?>
<prequest>
...
</prequest>
-----------------------------1987383936223
Content-Disposition: form-data; name="upload"
Upload
-----------------------------1987383936223--
我的C#代码
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http:/test-url:8080/Emul/report.jsp");
// here prequest-xml is contained
var bytes = File.ReadAllBytes(@"C:\NBCH\1.xml");
request.ContentLength = bytes.Length;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "text/xml; encoding='utf-8'";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();