简体   繁体   中英

Post method for HTTP Handler

I have a handler. When I call it with URL that is to say GET method, it works because I get values with my below handler code.

var encodedUrl = HttpUtility.UrlEncode(context.Request.QueryString.ToString());

How can I get values when I use post method which is below from Handler side:

        using (var wb = new WebClient())
        {
            var data = new NameValueCollection();
            data["a"] = "a";
            data["b"] = "b";

            var response = wb.UploadValues("http://localhost:126/Web", "POST", data);
        }

When you receive an http response you basically depend on the "Content Type". Depending on this type is that you read it. Here is a reference on this topic:

For instance if you decide to receive an "application/json" response type. You may be able to use this:

From I can see in your sample it looks like you are trying to implement an "application/x-www-form-urlencoded" and the post needs to be formatted accordingly. Here is a sample for that:

  1. http://msdn.microsoft.com/en-us/library/system.net.webclient.headers(v=vs.110).aspx
  2. How are parameters sent in an HTTP POST request?

But there are other options available. I hope this is the answer you were looking for.

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