在C#中将HttpClient与ASMX一起使用

编程入门 行业动态 更新时间:2024-10-28 21:28:43
本文介绍了在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一起使用

本文发布于:2023-11-14 13:14:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   HttpClient   ASMX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!