简体   繁体   English

无法使用System.Net.Http.HttpClient从REST Web服务获得有效响应

[英]Can't get a valid response from a REST web service using System.Net.Http.HttpClient

I am using this test method (and helper class) to verify the response from an external web service: 我正在使用此测试方法(和帮助程序类)来验证来自外部Web服务的响应:

[TestMethod]
public void WebServiceReturnsSuccessResponse()
{
    using (var provider = new Provider(new Info()))
    using (var result = provider.GetHttpResponseMessage())
    {
        Assert.IsTrue(result.IsSuccessStatusCode);
    }
}

private class Info : IInfo
{
    public string URL { get; set; } =
        "https://notreallythe.website.com:99/service/";
    public string User { get; set; } = "somename";
    public string Password { get; set; } = "password1";
}

I can't get this test to pass; 我无法通过这项测试; I always get a 500 - Internal Server Error result. 我总是得到500 - Internal Server Error结果。 I have connected via an external utility (Postman) - so the web service is up and I can connect with the url & credentials that I have. 我已经通过外部实用程序(邮递员)进行了连接-因此,Web服务已启动,并且可以使用我拥有的url和凭据进行连接。

I think the problem is in my instantiation of the HttpClient class, but I can't determine where. 我认为问题出在我的HttpClient类的实例中,但我无法确定在哪里。 I am using Basic authentication: 我正在使用基本身份验证:

public class Provider : IProvider, IDisposable 
{
    private readonly HttpClient _httpClient;

    public Provider(IInfo config){
        if (config == null) 
           throw new ArgumentNullException(nameof(config));

        var userInfo = new UTF8Encoding().GetBytes($"{config.User}:{config.Password}");

        _httpClient = new HttpClient
        {
            BaseAddress = new Uri(config.URL),
            DefaultRequestHeaders =
            {
                Accept = { new MediaTypeWithQualityHeaderValue("application/xml")},
                Authorization = new AuthenticationHeaderValue(
                   "Basic", Convert.ToBase64String(userInfo)),
                ExpectContinue = false,                 
            },
         };
    }

    public HttpResponseMessage GetHttpResponseMessage()
    {
        return _httpClient.GetAsync("1234").Result;
    }
}

The response I get back appears to go to the correct endpoint; 我返回的响应似乎到达了正确的端点; the RequestUri in the response looks exactly like I expect, https://notreallythe.website.com:99/service/1234 . 响应中的RequestUri看起来与我期望的完全一样, https://notreallythe.website.com:99/service/1234 //notreallythe.website.com:99/service/1234。

You need to load up Fiddler and do a recording of the HTTP traffic when this operation succeeds (through the browser). 当此操作成功(通过浏览器)时,您需要加载Fiddler并记录HTTP流量。

Then, load up your code, stand up another instance (or window) of Fiddler, and do the same thing with your code. 然后,加载您的代码,站立Fiddler的另一个实例(或窗口),并对您的代码执行相同的操作。 Now, compare the two Fiddler windows to see what is different. 现在,比较两个Fiddler窗口,看看有什么不同。

You only need to compare those things in Fiddler that are highlighted in blue. 您只需要比较Fiddler中以蓝色突出显示的那些内容。 You can ignore the other communications. 您可以忽略其他通信。

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

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