Is there a .NET ready made method to process response body of a HttpListener HttpListenerRequest body?

前端 未结 3 698
心在旅途
心在旅途 2021-02-07 07:05

I\'m using HttpListener to provide a web server to an application written in another technology on localhost. The application is using a simple form submission (application/x-w

3条回答
  •  粉色の甜心
    2021-02-07 07:14

    If you want to avoid the dependency on System.Web that is required to use HttpUtility.ParseQueryString, you could use the Uri extension method ParseQueryString found in System.Net.Http.

    Make sure to add a reference (if you haven't already) to System.Net.Http in your project.

    Note that you have to convert the response body to a valid Uri so that ParseQueryString (in System.Net.Http)works.

    string body = "value1=randomvalue1&value2=randomValue2";
    
    // "http://localhost/query?" is added to the string "body" in order to create a valid Uri.
    string urlBody = "http://localhost/query?" + body;
    NameValueCollection coll = new Uri(urlBody).ParseQueryString();
    

提交回复
热议问题