如何解决在 REST API 客户端中使用 GET 时抛出错误
我正在使用 REST 中的 Web 服务,我使用的是 Windows 窗体、C#。
当我跑线时
HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
出现错误
InnerException = {" 无法从传输连接读取数据:远程主机强制关闭现有连接。"}
如果有人能帮助我留下来,我将不胜感激。
代码如下:
public string Get()
{
string errorMessage;
string StrResponseValue = string.Empty;
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(EndPoint);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// List data response.
HttpResponseMessage response = client.GetAsync(urlParameters).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs.
if (response.IsSuccessstatusCode)
{
// Parse the response body.
var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result; // Make sure to add a reference to System.Net.Http.Formatting.dll
foreach (var d in dataObjects)
{
// Console.WriteLine("{0}",d.Name);
}
}
else
{
Console.WriteLine("{0} ({1})",(int)response.StatusCode,response.ReasonPhrase);
}
client.dispose();
}
catch(Exception ex)
{
errorMessage= ex.Message;
}
return StrResponseValue;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。