C#: HttpClient with POST parameters

前端 未结 2 447
时光取名叫无心
时光取名叫无心 2020-12-09 08:06

I use codes below to send POST request to a server:

string url = \"http://myserver/method?param1=1¶m2=2\"    
HttpClientHandler handler = new HttpCli         


        
2条回答
  •  执笔经年
    2020-12-09 08:48

    As Ben said, you are POSTing your request ( HttpMethod.Post specified in your code )

    The querystring (get) parameters included in your url probably will not do anything.

    Try this:

    string url = "http://myserver/method";    
    string content = "param1=1¶m2=2";
    HttpClientHandler handler = new HttpClientHandler();
    HttpClient httpClient = new HttpClient(handler);
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
    HttpResponseMessage response = await httpClient.SendAsync(request,content);
    

    HTH,

    bovako

提交回复
热议问题