C# 通过模拟http请求来调用soap、wsdl
C#调用webservice的方法很多,我说的这种通过http请求模拟来调用的方式是为了解决C#调用java的远程API出现各种不兼容问题。
由于远程API不在我们的控制下,我们只能修改本地的调用代码来适应远程API。
在以上情况下,我们就通过模拟http请求来去调用webservice。
首先,我们要分析调用端口时,我们发送出去的数据。
先抓个包看看,这里,我们没有办法用Fiddler来监听SOAP协议的内容,但是SOAP还是基于http协议的。
用更底层的工具是能够抓到的。这里可以去百度一下,工具很多。
不过我找了一个java写的,监听SOAP协议的小工具。《戳我下载》http://download.csdn.net/detail/a406502972/9460758
抓到包了之后,直接post就行了,简单易懂,直接上代码:
1 static string data = @"发送的内容"; 2 private static string contentType = "text/xml; charset=UTF-8"; 3 private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*"; 4 private static string userAgent = "Axis2"; 5 /// <summary> 6 /// 提交数据 7 /// </summary> 8 /// <param name="url"></param> 9 /// <param name="cookie"></param> 10 /// <param name="param"></param> 11 /// <returns></returns> 12 public static string PostWebContent(string url, CookieContainer cookie, string param) { 13 byte[] bs = Encoding.ASCII.GetBytes(param); 14 var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); 15 httpWebRequest.CookieContainer = cookie; 16 httpWebRequest.ContentType = contentType; 17 httpWebRequest.Accept = accept; 18 httpWebRequest.UserAgent = userAgent; 19 httpWebRequest.Method = "POST"; 20 httpWebRequest.ContentLength = bs.Length; 21 using (Stream reqStream = httpWebRequest.GetRequestStream()) { 22 reqStream.Write(bs, 0, bs.Length); 23 } 24 var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 25 Stream responseStream = httpWebResponse.GetResponseStream(); 26 string html = ""; 27 if (responseStream != null) { 28 StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); 29 html = streamReader.ReadToEnd(); 30 streamReader.Close(); 31 responseStream.Close(); 32 httpWebRequest.Abort(); 33 httpWebResponse.Close(); 34 } 35 return html; 36 }
作者:Li keli
出处:https://www.cnblogs.com/likeli/p/5275279.html
本站使用「署名 4.0 国际」创作共享协议,转载请在文章明显位置注明作者及出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?