简体   繁体   English

httpClient.PostAsync返回“不允许使用方法”(在winform应用程序中运行)

[英]httpClient.PostAsync returns “Method Not Allowed” (running in winform app)

I try to post to rest. 我尝试张贴休息。

This is my code: 这是我的代码:

     string URL = "http://xxx.xxx.x.xx:8080/Name/NAME/";
     string urlParameters = "?key=T_PAPPS&value=sofsof";

     HttpClient client = new HttpClient();
     client.BaseAddress = new Uri(URL);

     client.DefaultRequestHeaders.Accept.Add(
     new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

     HttpResponseMessage response = client.PostAsync(URL, new StringContent(urlParameters)).Result;

My app is WinForm application (not web). 我的应用程序是WinForm应用程序(不是Web)。

What am I doing wrong? 我究竟做错了什么?

Thanks for advance. 感谢前进。

(restsharp returns same error (and there I also didn't success to change to application/x-www-form-urlencoded from application/json)) (restsharp返回相同的错误(并且我也没有成功从application / json更改为application / x-www-form-urlencoded))

If you're using querystring parameters (ie ?key=value) you do not need to post. 如果您使用的是querystring参数(即?key = value),则无需发布。 Try replacing your last line with: 尝试将最后一行替换为:

HttpResponseMessage response = client.GetAsync(URL + urlParameters).Result;

You may also need to remove the final "/" in your URL string. 您可能还需要在URL字符串中删除最后的“ /”。

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

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