我需要向网站或网络服务器发送一个POST请求,但它必须是原始格式(如fiddler十六进制视图中所示)然后获得响应。它是一个字节数组,我已经拥有(并且始终是相同的)带有标题,cookie,值,所有设置的“POST ....”。
我希望像SendRawBytes(url, bytes);
这样的函数只发送“bytes”变量。
我已经尝试了WebClient.UploadData(url, data)
,但它在请求中添加了POST,HOST,Content-Length和Expect,结果如下:
POST http://mail.mymail.com/index.html/?_task=login HTTP/1.1
Host: mail.mymail.com
Content-Length: 1000
Expect: 100-continue
POST http://mail.mymail.com/index.html/?_task=login HTTP/1.1
Host: mail.mymail.com
Connection: keep-alive
Content-Length: 148
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://mail.mymail.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://mail.mymail.com/index.html/
Accept-Encoding: gzip, deflate
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: __utma=212529476.250438617.1448317607.1448937588.1448974476.6; __utmc=212529476; __utmz=212529476.1448317607.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
_task=login_url=&_user=testuser&_pass=01234
一切都会有很大的帮助!
提前谢谢。
答案 0 :(得分:2)
如果要发送原始字节,则无法使用HTTP库。您需要打开TcpClient
并手动发送字节。
答案 1 :(得分:-1)
以下是我们将字节写入请求的示例:
请注意,我使用的是HttpWebRequest而不是WebClient。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_loginURL);
request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; MS-RTC LM 8; .NET4.0C; .NET4.0E)";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Headers.Set(HttpRequestHeader.Cookie, @"__utma=185713471.13350290.1355320152.1355320152.1355324618.2; __utmz=185713471.1355320152.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); JSESSIONID=" + _jessionID);
request.Headers.Set(HttpRequestHeader.CacheControl, "no-cache");
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-gb");
request.Proxy = ProxyFactory.GetWebProxy();
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
string body = string.Format(@"%7BactionForm.username%7D={0}&%7BactionForm.password%7D={1}", _username, _password);
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
response = (HttpWebResponse)request.GetResponse();
要查看的主要部分是request.GetRequestStream()
希望这有帮助。