【问题标题】:HttpClient.SendAsync: 500 - Internal Server Error in Windows Service ApplicationHttpClient.SendAsync:500 - Windows 服务应用程序中的内部服务器错误
【发布时间】:2015-11-07 01:47:23
【问题描述】:

我在尝试将 json 字符串发布到 Web API 时看到以下消息(这是写入包含 System.IO.File.WriteAllText 的行中的文件的内容):

Response: StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  Cache-Control: no-cache
  Date: Thu, 13 Aug 2015 21:26:12 GMT
  Server: Microsoft-IIS/8.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 36
  Content-Type: application/json; charset=utf-8
  Expires: -1
}

我的异步发布方法中的代码如下:

  Uri theUri = new Uri("http://www.website.com/WebsiteAPI/PostDetails");

  HttpClient aClient = new HttpClient();
  aClient.BaseAddress = theUri;
  aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  aClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
      Convert.ToBase64String(
      System.Text.ASCIIEncoding.ASCII.GetBytes(
      string.Format("{0}:{1}", "USERNAME", "PASSWORD"))));
  aClient.DefaultRequestHeaders.Host = theUri.Host;

  HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, theUri);
  request.Content = new StringContent(resource, Encoding.UTF8, "application/json");

  // Post the data 
  aClient.SendAsync(request).ContinueWith(responseTask =>
      {
          System.IO.File.WriteAllText(PROFILE_JSON_PATH, "Response: " + responseTask.Result);
      });

我在网上尝试了许多不同的解决方案,但似乎没有任何效果。知道为什么这可能不起作用吗?

【问题讨论】:

    标签: c# asp.net json http


    【解决方案1】:

    简短的回答是您的 Web API 存在错误。您的客户端代码可能有问题,也可能没有问题 - 但您必须访问 Web API 以获取可以解释问题所在的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 2019-05-28
      • 2017-08-15
      • 2022-01-12
      • 1970-01-01
      相关资源
      最近更新 更多