Created
December 16, 2014 13:45
-
-
Save ludo6577/99009050b199179d6e25 to your computer and use it in GitHub Desktop.
call HttpPost method from Client in C# (http://stackoverflow.com/questions/26990773/call-httppost-method-from-client-in-c-sharp-code/26990889#26990889)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var client = new HttpClient()) | |
{ | |
var values = new List<KeyValuePair<string, string>>(); | |
values.Add(new KeyValuePair<string, int>("n", "42")); | |
values.Add(new KeyValuePair<string, string>("s", "string value")); | |
var content = new FormUrlEncodedContent(values); | |
var response = await client.PostAsync("http://www.domain.org/receiver.aspx", content); | |
var responseString = await response.Content.ReadAsStringAsync(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = (HttpWebRequest)WebRequest.Create("http://www.domain.org/receiver.aspx"); | |
var postData = "n=42&s=string value"; | |
var data = Encoding.ASCII.GetBytes(postData); | |
request.Method = "POST"; | |
request.ContentType = "application/x-www-form-urlencoded"; | |
request.ContentLength = data.Length; | |
using (var stream = request.GetRequestStream()) | |
{ | |
stream.Write(data, 0, data.Length); | |
} | |
var response = (HttpWebResponse)request.GetResponse(); | |
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var wb = new WebClient()) | |
{ | |
var data = new NameValueCollection(); | |
data["n"] = "42"; | |
data["s"] = "string value"; | |
var response = wb.UploadValues("http://www.domain.org/receiver.aspx", "POST", data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WebRequest request = WebRequest.Create ("http://www.domain.org/receiver.aspx"); | |
request.Method = "POST"; | |
string postData = "n=42&s=string value"; | |
byte[] byteArray = Encoding.UTF8.GetBytes (postData); | |
request.ContentType = "application/x-www-form-urlencoded"; | |
request.ContentLength = byteArray.Length; | |
Stream dataStream = request.GetRequestStream (); | |
dataStream.Write (byteArray, 0, byteArray.Length); | |
dataStream.Close (); | |
//Response | |
WebResponse response = request.GetResponse (); | |
Console.WriteLine (((HttpWebResponse)response).StatusDescription); | |
dataStream = response.GetResponseStream (); | |
StreamReader reader = new StreamReader (dataStream); | |
string responseFromServer = reader.ReadToEnd (); | |
Console.WriteLine (responseFromServer); | |
reader.Close (); | |
dataStream.Close (); | |
response.Close (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment