简体   繁体   中英

Accepting URL Encoded POST Request with C# in ASP.NET

I am working with a 3rd party that wants to send info via url encoded post requests and I can't seem to figure out how to accept the data when they send the request.

Here is what their server log says after submitting the request (with some changes made to take my address and the information they were sending out):

[13-01-12 14:52:55][DEBUG]: >> "POST /mypostpage.aspx HTTP/1.1[\r][\n]"
[13-01-12 14:52:55][DEBUG]: >> "User-Agent: Jakarta Commons-HttpClient/3.0.1[\r][\n]"
[13-01-12 14:52:55][DEBUG]: >> "Host: mydomain.com[\r][\n]"
[13-01-12 14:52:55][DEBUG]: >> "Content-Length: 1034[\r][\n]"
[13-01-12 14:52:55][DEBUG]: >> "Content-Type: application/x-www-form-urlencoded[\r]   [\n]"
[13-01-12 14:52:55][DEBUG]: >> "[\r][\n]"
[13-01-12 14:52:55][DEBUG]: >>     "Variable1=variable1value&variable2=variable2value&variable3=variable3value"
[13-01-12 14:53:02][DEBUG]: << "HTTP/1.1 200 OK[\r][\n]"

To grab the data they are sending, I've been trying to use Request.QueryString. So to get the value for variable one I might write something like:

variable1 = Request.QueryString["variable1"];

I'm sure that I'm missing something blatantly obvious, but I haven't been able to find an answer on why this hasn't been working. When I test it on my own it works fine, but when they submit the requests, it doesn't work at all, the request comes through as though there was nothing in it.

Request.QueryString extracts query string variables from the request's URI, which are typically encoded using the application/x-www-form-urlencoded scheme. Query string parameters will not get POSTed information.

While the content type of the POSTed data above uses the same scheme as query string variables, in order to access them you need to use variable1 = Request.Form["variable1"]; or, alternatively, variable1 = Request.Params["variable1"]; which contains a cumulative collection of POSTed values, query string parameters, cookies and server variables.

For reference: Request.Params or Request.Form

POST and GET requests are conceptually different even though a POST request with application/x-www-form-urlencoded variables is identical in format to a GET request with query string parameters formatted with the same scheme; hence the separation of access methods within the HttpRequest class.

EDIT:

The HttpRequest.Form and HttpRequest.Params properties return a NameValueCollection , which associates string values with a string-based key, thus all values will be strings.

string variable1 = Request.Form["variable1"];

The content-type they are using is one of the two formats that HTML forms use with method="post" , the more common of the two in fact, and Request.Form parses both.

Indeed, the content-type was probably picked to make life easy on people at your end of the transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM