I need to submit data via POST request to a third party api, I have the url to submit to, but I am trying to avoid first sending the data to the client-side and then posting it again from there. Is there a way to POST information from codebehind directly?
Any ideas are appreciated.
Something like this?
string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1¶m2=value2¶m3=value3";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
}
How to post data to specific URL using WebClient in C#
From the server side you can post it to the url.
See the sample code from previous stackoverflow question - HTTP request with post
using (var wb = new WebClient())
{
var data = new NameValueCollection();
data["username"] = "myUser";
data["password"] = "myPassword";
var response = wb.UploadValues(url, "POST", data);
}
Use the WebRequest class to post.
http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx
Alternatively you can also use HttpClient class:
http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx
Hope this helps. Please post if you are facing issues.
You should to use WebRequest class:
var request = (HttpWebRequest)WebRequest.Create(requestedUrl);
request.Method = 'POST';
using (var resp = (HttpWebResponse)request.GetResponse()) {
// your logic...
}
Full info is here https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With