sinosoidal:How can i do such POST requests from C#?
You can use either HttpWebRequest or XmlHttpRequest Wrapper.
The following code is for HttpWebRequest.
private void InvokeAsync() {
//http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx
string serviceURL = "http://localhost:52976/YourServices.php";
pMessage = "ProductName=\"jetli\"";
// Create a new HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceURL);
// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";
// Start the asynchronous operation.
request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);
// Keep the main thread from continuing while the asynchronous
// operation completes. A real world application
// could do something useful such as updating its user interface.
allDone.WaitOne();
// Get the response.
request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
}
private static void ResponseCallback(IAsyncResult asynchronousResult) {
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = resp.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine(responseString);
// Close the stream object.
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse.
resp.Close();
}
private static void ReadCallback(IAsyncResult asynchronousResult) {
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation.
Stream postStream = request.EndGetRequestStream(asynchronousResult);
string postData = pMessage;
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Write to the request stream.
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
allDone.Set();
}
You can also read this post. http://www.talentgrouplabs.com/blog/archive/2008/03/11/asynchronous-http-post-request-with-silverlight-2.0-beta-1.aspx
(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)
Best Regards,
Michael Sync
Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed