在C#中将HttpClient与ASMX一起使用 [英] Using HttpClient with ASMX in C#
本文介绍了在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", "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;
这篇关于在C#中将HttpClient与ASMX一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文