Memmed Ehmedov
Memmed Ehmedov

Reputation: 1

how to send POST without waiting response C#

how to send POST without waiting response in C#? How can I do it?

Help please!

Upvotes: 0

Views: 9322

Answers (4)

christutty
christutty

Reputation: 952

UploadValuesAsync looks like a more complete solution, using something like this:

using (var client = new WebClient())
{
    var values = new NameValueCollection();
    // add values...
    client.UploadValuesAsync(new System.Uri(rawUrl), "POST", values);
}

However note that I haven't tested this code as I was trying to solve a slightly different problem.

Upvotes: 1

D.j.
D.j.

Reputation: 106

An HTTP session is just a standard TCP session, and a POST is just a properly formatted piece of data that meets the HTTP specification. You can open a TCP connection to the web server and send a POST request and then close the TCP connection. You can check the HTTP specification (RFC 2616) to learn how to properly format a POST. You can find it at: http://www.w3.org/Protocols/rfc2616/rfc2616.html

A very basic POST would be sending the following over a TCP connection:

POST / HTTP/1.1
Host: www.thehost.com
Content-Length: 3
Content-Type: application/x-www-form-urlencoded

Hi!

Substituting the content, and the corresponding length with whatever you want to send, changing the / to the correct path and putting the hostname or IP of the host at Host:... and connecting the TCP session to that host of course.

A very very basic example:

using System.Net;
using System.Net.Sockets;


static void Main(string[] args)
{
    string Hostname = "www.website.com";
    TcpClient Client = new TcpClient(Hostname, 80);
    Client.Client.Send(new ASCIIEncoding().GetBytes("POST / HTTP/1.1\nHost: "+Hostname+"\nConnection: close\n\n"));
    Client.Close();
}

Changing www.website.com to the correct hostname (and the port if required).

Upvotes: 2

Jonas Sourlier
Jonas Sourlier

Reputation: 14455

var wc = new WebClient();
wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
wc.UploadStringAsync(uri, data);
wc.UploadStringCompleted += (sender, e) =>
{
    // completed
};

Upvotes: 0

Guffa
Guffa

Reputation: 700592

Use one of the asynchronous methods in the WebClient class, for example UploadStringAsync.

The corresponding event (UploadStringCompleted for the example) will be triggered when the post is completed (or fails), but you don't have to hook up any handler for the event. You might want to do that, however, to check if there was any error.

Upvotes: 3

Related Questions