c#中用HttpWebResponse类进行Post发包,发包的数据格式是multipart/form-data;
有两个网址:
http://hpcgi2.nifty.com/tono-k/bbs/bbs.cgi
http://handaipfc.com/cgi-bin/imgboard.cgi
我已经写好了发包的方法,测试第一地址是发包成功的,但是对第二个地址却不成功,不知道什么原因,有没有高手帮忙看一下.
发包的数据:
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="bbsaction"
post
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="page"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="view_mode"
as_cgi_defined
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="blood"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="parent"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="prebbsaction"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="amode"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="p1"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="p2"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="target"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="target_no"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="sf"
針つゆシ灰み
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="onetime_token"
aSB2BUxsBU
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="optB"
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="body"
昔を追憶する
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="name"
昔を追憶する
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="subject"
昔を追憶する
-----------------------------8d3bb8669ffe4f1
Content-Disposition: form-data; name="img"; filename=""
Content-Type: application/octet-stream
-----------------------------8d3bb8669ffe4f1--
private static string HttpPostTest(string url, int timeOut, NameValueCollection stringDict)
{
string responseContent;
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(url);
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
// 边界符
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
// 设置属性
webRequest.Method = "POST";
webRequest.Timeout = timeOut;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
memStream.Write(beginBoundary, 0, beginBoundary.Length);
// 写入字符串的Key
var stringKeyHeader = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}\r\n";
foreach (byte[] formitembytes in from string key in stringDict.Keys
select string.Format(stringKeyHeader, key, stringDict[key])
into formitem
select Encoding.UTF8.GetBytes(formitem))
{
memStream.Write(formitembytes, 0, formitembytes.Length);
}
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
webRequest.ContentLength = memStream.Length;
string sss = System.Text.Encoding.Default.GetString(memStream.ToArray());
var requestStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("shift_jis")))
{
responseContent = httpStreamReader.ReadToEnd();
}
httpWebResponse.Close();
webRequest.Abort();
return responseContent;
}
你的地址没有,你的检查代码看下做了什么现在。不是自己用浏览器提交一次,看http头都包含了什么,如cookie之类,自定义其他的数据什么的,如防止crsf的token之类的
private static string HttpPostTest(string url, string boundary, string postData, string cookie)
{
string responseContent;
var webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers[HttpRequestHeader.Cookie] = cookie;
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webRequest.Method = "POST";
webRequest.Timeout = 600000;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
byte[] buffer = Encoding.UTF8.GetBytes(postData);
webRequest.ContentLength = buffer.Length;
webRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("shift_jis")))
{
responseContent = httpStreamReader.ReadToEnd();
}
httpWebResponse.Close();
webRequest.Abort();
return responseContent;
}
请求头
POST /cgi-bin/imgboard.cgi HTTP/1.1
Host: handaipfc.com
Connection: keep-alive
Content-Length: 1965
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://handaipfc.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarybMSInkSagMemRRQm
Referer: http://handaipfc.com/cgi-bin/imgboard.cgi
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
第二个地址却不成功,第二个地址是哪个?提示什么?
解决自己解决了,编码问题,要换成日语的,UTF8不行