9

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.

1

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

Comments

9

Something like this?

string URI = "http://www.myurl.com/post.php"; string myParameters = "param1=value1&param2=value2&param3=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#

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.