简体   繁体   English

在C#中使用HttpClient和ASMX

[英]Using HttpClient with ASMX in C#

I am playing with HttpClient'. I'm wondering if it is compatible with 我正在玩HttpClient'. I'm wondering if it is compatible with HttpClient'. I'm wondering if it is compatible with .asmx` web services. HttpClient'. I'm wondering if it is compatible with .asmx`网络服务HttpClient'. I'm wondering if it is compatible with

Say I have a method such as this: 假设我有一个这样的方法:

        var baseAddress = new Uri("Http://localhost/folder/WebServiceCommand.asmx/");          
        var config = new HttpSelfHostConfiguration(baseAddress);                      
        var server = new HttpSelfHostServer(config);

        using (var client = new HttpClient(server))
        {
            client.BaseAddress = baseAddress;

            var response = client.GetAsync("GetData").Result;

            Assert.IsTrue(
                        response.IsSuccessStatusCode, 
                        "Actual status code: " + response.StatusCode);
        }

Firstly, is what I'm trying to do possible or should I use the older HttpWebRequest class? 首先,我正在尝试做什么或者我应该使用旧的HttpWebRequest类?

If it is possible, why is it returning a 404 - NOT FOUND? 如果有可能,为什么它会返回404 - 未找到? If I paste the address into a browser, it is found OK. 如果我将地址粘贴到浏览器中,则会发现确定。

Thanks 谢谢

You can use the HttpClient with a Web Service ASMX endpoint 您可以将HttpClient与Web Service ASMX端点一起使用

        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
        httpClient.DefaultRequestHeaders.Add("SOAPAction", "http://foo.com/GetVersion");

        var soapXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetVersion xmlns=\"http://foo.com/\" /></soap:Body></soap:Envelope>";

        var response = httpClient.PostAsync("http://foo.com/bar.asmx", new StringContent(soapXml, Encoding.UTF8, "text/xml")).Result;

        var content = response.Content.ReadAsStringAsync().Result;

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

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