在没有身份验证的情况下发出POST请求C#

问题描述 投票:0回答:1

我想向REST服务发出POST请求。没有身份验证,它只有两个自定义的标头。我的代码如下。我收到错误消息:

mscorlib.dll中发生类型'System.AggregateException'的异常,但未在用户代码中处理。

“连接尝试失败,因为连接方在一段时间后未正确响应,或者建立的连接失败,因为连接的主机未能响应”

您可以帮忙吗?代码有什么问题?

 HttpClient client = new HttpClient();
 client.DefaultRequestHeaders.Accept.Clear();
 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 client.DefaultRequestHeaders.Add("id", "8888");
 client.DefaultRequestHeaders.Add("type", "CUSTOMER");
 Uri uri = new Uri(requestUri);
 var ob = new { id= "5", color= "pink" };
 var transferJson = JsonConvert.SerializeObject(ob);
 var content = new StringContent(transferJson, Encoding.UTF8, "application/json");
 HttpResponseMessage responseMessage = client.PostAsync(uri, content).Result;
c# rest post httprequest httpclient
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.