C#将原始字节数组发送到网站
本文关键字:网站 数组 字节数 原始 字节 | 更新日期: 2023-09-27 18:25:31
我需要向网站或Web服务器发送POST请求,但它必须是原始形式(如fiddler十六进制视图中所示),然后才能得到响应。这是一个字节数组,我已经有了(而且一直都是一样的),带有"POST……",包含头、cookie、值和所有设置。
我期望像SendRawBytes(url, 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
任何事情都会有很大帮助!
提前谢谢。
如果要发送原始字节,则不能使用HTTP库。您需要打开一个TcpClient
并手动发送字节。
以下是我们向请求写入字节的示例:
请注意,我使用的是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();
要查看的主要部分是请求。GetRequestStream()
希望这能有所帮助。