Reputation: 571
How to send data to authenticate to the server? Using the current code:
WebClient MyWebClient = new WebClient();
MyWebClient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(MyWebClient_OpenWriteCompleted);
MyWebClient.Headers["User-Agent"] = "Mozilla/4.0 (compatible; ICS)";
MyWebClient.OpenWriteAsync(new Uri("http://myserver.com/login"), "POST", "[email protected]&pass=mypassword");
void MyWebClient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
{
throw new NotImplementedException();
}
After this code, if a program-sniffer (HttpAnalyzer) look at Headers and Response Content, in Response Content will be written: Request is not completed. waiting ...
, and Response Headers will be empty ...
How to make a Post request?
Upvotes: 0
Views: 2689
Reputation: 450
//Making a POST request using WebClient.
Function()
{
WebClient wc = new WebClient();
var URI = new Uri("http://your_uri_goes_here");
//If any encoding is needed.
wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
//Or any other encoding type.
//If any key needed
wc.Headers["KEY"] = "Your_Key_Goes_Here";
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
wc.UploadStringAsync(URI,"POST","Data_To_Be_sent");
}
void wc__UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
try
{
MessageBox.Show(e.Result);
//e.result fetches you the response against your POST request.
}
catch(Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
Upvotes: 0
Reputation: 7879
According to doc, contents are actually sent when you close the stream passed to you in the argument of OpenWriteCompleted
event.
Since you don't close the stream properly, it doesn't send anything.
Upvotes: 1