在 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 (将#修改为@)