简体   繁体   English

具有参数的Httpclient POST

[英]Httpclient POST with Parameters

I'm really stuck to send a POST request by HttpClient with Parameters: 我真的很想通过HttpClient发送带有参数的POST请求:

    private async Task<string> PostAsync(string parameterValue)
    {
        var ResponseMessage = await HttpClient.PostAsyncAsync(myUri, new FormUrlEncodedContent(
            new[]
            {
                new KeyValuePair<string, string>("parameter", parameterValue)

Thanks - })); 谢谢 - })); var httpResponse = await Deserialize(ResponseMessage); var httpResponse =等待Deserialize(ResponseMessage);

        return Response.Token;
    }

What I get is POST https://URI with parameters in the content HTTP/1.1 and what I need is https://URI?parameter= PARAMETERVALUE HTTP/1.1 我得到的是POST https:// URI ,内容为HTTP / 1.1,而我需要的是https:// URI?parameter = PARAMETERVALUE HTTP / 1.1

Can anybody help me? 有谁能够帮助我?

EDIT: 编辑:

Thanks - It's just like: 谢谢-就像:

    private async Task<string> PostAsync(string parameterValue)
    {
        var ResponseMessage = await HttpClient.PostAsyncAsync(string.Format(myUri?parameter={0}, parameterValue), null)

The method you are using takes everything in the 2nd parameter and uses it for HttpContent. 您正在使用的方法将第二个参数中的所有内容都用作HttpContent。 You can read more here . 您可以在这里阅读更多内容。 What you should be doing is appending the parameters to myUri variable. 您应该做的是将参数附加到myUri变量。

You shouldn't be passing values in the URL, this likely goes into an encoded body section (accessible via the request stream of an HttpWebRequest ). 您不应该在URL中传递值,这可能会进入已编码的正文部分(可通过HttpWebRequest的请求流进行访问)。 I would suggest a tool such as Fiddler in order to analyze the type of POST request you are trying to make, so that you will know what kind of data needs to go into the body, the encoding type, protocols, etc. 我建议使用诸如Fiddler之类的工具来分析您要发出的POST请求的类型,以便您知道将哪种数据输入主体,编码类型,协议等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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