Reputation: 239
I need to send to a website or webserver a POST request, but it must be in its raw form (as seen in fiddler hex view) and then get the response. It is a byte array that I already have (and is always the same) with the "POST ...." with headers, cookies, values, everything set up.
I expect a function like SendRawBytes(url, bytes);
that sends only the "bytes" variable.
I've already tried WebClient.UploadData(url, data)
but it adds the POST, HOST, Content-Length and Expect to the request, which results in this:
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
Anything would be of great help!
Thank you in advance.
Upvotes: 2
Views: 1813
Reputation: 806
Here is a sample of where we write bytes to the request:
Note that I am using HttpWebRequest rather than 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();
The main part to look at is the request.GetRequestStream()
Hopefully this helps.
Upvotes: -1
Reputation: 171178
If you want to send raw bytes you can't use an HTTP library. You need to open a TcpClient
and send the bytes manually.
Upvotes: 2