2

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.

4
  • 1
    did you take a look at udpclient send msdn.microsoft.com/en-us/library/08h8s12k(v=vs.110).aspx ? the problem is taht web clients ALWAYS encode it so that its a valid web request (which normally has some headers). udp on the other hand does not care at all. Commented Dec 2, 2015 at 14:35
  • What are you trying to accomplish? What is consuming the request? You said you need to POST data to a webserver, but in order for the webserver to recognize the data as a valid POST, it must include the proper headers. A webserver doesn't have any inherent manner of interpreting arbitrary binary data sent at it. Commented Dec 2, 2015 at 14:40
  • Sorry, I don't think I've expressed myself the way I wanted. I've just edited my question to add more details about the problem @DVK Commented Dec 2, 2015 at 14:57
  • What is consuming the request? Is it some kind of socket, or are you just trying to perform a post to a web page and get something back? Any details on that end? Any web page expecting data to be posted to it should handle headers just find. If you are attempting to post a file to it, make sure you set the content type of your request to the appropriate value. I assume that the second code snippet posted is the response, in which case it tells you exactly what it will accept as a content type: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 Commented Dec 2, 2015 at 16:10

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

5 Comments

Please do not down vote without providing a reason... The OP asked for a point in the right direction which I believe this snippet provides.
Right, I should have commented. He has the HTTP request including headers as bytes. This code wraps that request in another, just like he already has.
In this case I write bytes to the body. I don't see anywhere that OP states bytes should be in the header (unless I'm misreading). My point stands that this may (or may not) help point OP in correct direction.
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. sounds pretty clear to me.
It did help me, but TcpClient was exactly what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.