m arcs kumar m arcs kumar - 1 year ago 106
C# Question

Post request using tcp level

I am able to login to a site (eaxmple.com) using webclient. but it is slower when site user is heavy. so i think to go with lower level.

I need suggestion is is good to go with tcpclient in c#. i need to manage cookie also after login. i tried to study this class from msdn but could not understand how to do post and get request using tcpclient.

i tried this for get request. but dont know about post and cookie management

byte[] buf = new byte[1024];
string header = "GET /?scope=images&nr=1 HTTP/1.1\r\n" +
"Host: www.bing.com\r\n" +
"Connection: keep-alive\r\n" +
"User-Agent: Mozilla/5.0\r\n" +

var client = new TcpClient();
client.Connect("www.bing.com", 80);

// send request
var stream = client.GetStream();
var streamReader = new StreamReader(stream);
var streamWriter = new StreamWriter(stream);


// get response
var response1 = streamReader.ReadLine();

Please suggest and provide me a link where i can start this.

Answer Source

Briefly, to send a POST request, simply replace the verb 'GET' with 'POST' and there you go. To add actual payload (like form data) to the request, append a second line break at the end of your header variable and then the actual data in base64 format. Cookies are added as headers. Just google for 'POST HTTP format' and look for example code, there must be dozens. An HTTP request is just a sequence of characters -- at least, you can start with it.

However, 'going down one level' won't help you if you have bandwidth or server load issues. Also, it's not easy to build a fully functional HTTP client. Lots of work for nothing.