How to POST data to ASP.NET HttpHandler?

前端 未结 6 731
无人及你
无人及你 2021-02-02 15:58

I am trying to send a large chunk of data over to a HTTP handler. I can\'t send it using GET because of the URL length limit so I decided to POST it instead. The problem is that

相关标签:
6条回答
  • 2021-02-02 16:27

    Having some code to look at would help diagnose the issue. Have you tried something like this?

    jQuery code:

    $.post('test.ashx', 
           {key1: 'value1', key2: 'value2'}, 
           function(){alert('Complete!');});
    

    Then in your ProcessRequest() method, you should be able to do:

    string key1 = context.Request.Form["key1"]; 
    

    You can also check the request type in the ProcessRequest() method to debug the issue.

    if(context.Request.RequestType == "POST")
    {
        // Request should have been sent successfully
    }
    else
    {
        // Request was sent incorrectly somehow
    }
    
    0 讨论(0)
  • 2021-02-02 16:43

    POST fields are contained in

    HttpContext.Request.Params
    

    To retrieve them you can use

    var field = HttpContext.Request.Params["fieldName"];
    
    0 讨论(0)
  • 2021-02-02 16:46

    Faced similar problem. After correcting all issues, there was one more thing I missed in web.config - to change verb as * OR GET,POST. After that everything worked fine.

    <httpHandlers>
        ...
        <add verb="*" path="test.ashx" type="Handlers.TestHandler"/>
    </httpHandlers>
    
    0 讨论(0)
  • 2021-02-02 16:51

    I was having the same problem, and eventually figured out that setting the content type as "json" was the issue...

    contentType: "application/json; charset=utf-8"
    

    That's a line some popular tutorials suggest you to add in the $ajax call, and works well with ASPx WebServices, but for some reason it doesn't for an HttpHandler using POST.

    Hard to catch since values in the query string work fine (another technique seen in the web, though it doesn't makes much sense to use POST for that).

    0 讨论(0)
  • 2021-02-02 16:52

    I also had the same problem. It was an client/AJAX problem. I had to set the AJAX call request header "ContentType" to

    application/x-www-form-urlencoded
    

    to make it work.

    0 讨论(0)
  • 2021-02-02 16:54

    The POST data that you are sending to your HTTP Handler must be in querystring format a=b&c=d. And you can retrieve it on the server-side using Request["a"] (will return b), and so on.

    0 讨论(0)
提交回复
热议问题