本文介绍了在C#中将HttpClient与ASMX一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用HttpClient'. I'm wondering if it is compatible with .asmx`网络服务.
I am playing with HttpClient'. I'm wondering if it is compatible with.asmx` web services.
说我有一个这样的方法:
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); }首先,我正在尝试做些什么吗?还是应该使用旧的HttpWebRequest类?
Firstly, is what I'm trying to do possible or should I use the older HttpWebRequest class?
如果有可能,为什么返回404-NOT FOUND?如果我将地址粘贴到浏览器中,就可以了.
If it is possible, why is it returning a 404 - NOT FOUND? If I paste the address into a browser, it is found OK.
谢谢
推荐答案您可以将HttpClient与Web Service ASMX端点一起使用
You can use the HttpClient with a Web Service ASMX endpoint
var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); httpClient.DefaultRequestHeaders.Add("SOAPAction", "foo/GetVersion"); var soapXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"www.w3/2001/XMLSchema-instance\" xmlns:xsd=\"www.w3/2001/XMLSchema\" xmlns:soap=\"schemas.xmlsoap/soap/envelope/\"><soap:Body><GetVersion xmlns=\"foo/\" /></soap:Body></soap:Envelope>"; var response = httpClient.PostAsync("foo/bar.asmx", new StringContent(soapXml, Encoding.UTF8, "text/xml")).Result; var content = response.Content.ReadAsStringAsync().Result;更多推荐
在C#中将HttpClient与ASMX一起使用
发布评论