HttpWebRequest,HttpWebResponse C# 代码调用webservice,参数为xml
程序员文章站
2022-06-23 16:09:37
配置文件中红色标注是必填项。 ......
-
先上调用代码
1 public static string PostMoths(string url, string Json) 2 { 3 System.Net.HttpWebRequest request; 4 Stream writer; 5 System.Net.HttpWebResponse response; 6 7 try 8 { 9 string strURL = url; 10 System.GC.Collect(); 11 System.Net.ServicePointManager.Expect100Continue = false; 12 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); 13 System.Net.ServicePointManager.DefaultConnectionLimit = 1024; 14 request.ServicePoint.ConnectionLimit = 200; 15 request.KeepAlive = false; 16 request.Timeout = 1000 * 60 * 5; 17 request.Method = "POST"; 18 request.ProtocolVersion = HttpVersion.Version11; 19 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 20 request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; 21 string paraUrlCoded = Json; 22 byte[] payload; 23 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); 24 request.ContentLength = payload.Length; 25 writer = request.GetRequestStream(); 26 writer.Write(payload, 0, payload.Length); 27 28 response = (System.Net.HttpWebResponse)request.GetResponse(); 29 System.IO.Stream s; 30 s = response.GetResponseStream(); 31 string strValue = ""; 32 StreamReader Reader = new StreamReader(s, Encoding.UTF8); 33 strValue = Reader.ReadToEnd(); 34 if (request != null) 35 { 36 request.Abort(); 37 request = null; 38 } 39 if (response != null) 40 { 41 response.Close(); 42 response = null; 43 } 44 if (writer != null) 45 { 46 writer.Close(); 47 writer = null; 48 } 49 return strValue; 50 } 51 catch (Exception ex) 52 { 53 var obj = new 54 { 55 status = 0, 56 msg = ex.Message 57 }; 58 request = null; 59 response = null; 60 writer = null; 61 return ex.Message; 62 } 63 finally 64 { 65 request = null; 66 response = null; 67 writer = null; 68 } 69 }
-
配置文件信息 WebConfig
1 <system.web> 2 <!--解决本地调用成功,外网调用失败的问题--> 3 <webServices> 4 <protocols> 5 <add name="HttpSoap"/> 6 <add name="HttpPost"/> 7 <add name="HttpGet"/> 8 <add name="Documentation"/> 9 </protocols> 10 </webServices> 11 <!--上传文件大小限制--> 12 <httpRuntime executionTimeout="300" maxRequestLength="100960" useFullyQualifiedRedirectUrl="false" requestValidationMode="2.0"/> 13 <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" validateRequest="false"/> 14 </system.web>
配置文件中红色标注是必填项。
-
webservice接口代码
1 [WebService(Namespace = "http://tempuri.org/")] 2 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 3 //[System.ComponentModel.ToolboxItem(false)] 4 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 5 [System.Web.Script.Services.ScriptService] 6 public class MESService : System.Web.Services.WebService 7 { 8 [WebMethod(Description = "")] 9 public void GetData(string strXML) 10 { 11 //你的结果处理 12 #region 处理数据 13 14 #endregion 15 Context.Response.Write(Common.JsonHelper.SerializeToJson(new { status = 111, msg = strXML })); 16 Context.Response.End(); 17 } 18 }
-
接口参数
1 <?xml version="1.0" encoding="UTF-8"?> 2 <LineEqui 3 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 5 <LineCode>W1</LineCode> 6 <EquiCode>qwe</EquiCode> 7 <PlanStatus>Running</PlanStatus> 8 <DataTime>2018-02-02 09:34:16</DataTime> 9 <FactoryCode>2222</FactoryCode> 10 <ProductCode>444</ProductCode> 11 <PlanCount>10000</PlanCount> 12 <Unit>PC</Unit> 13 <Batch>123</Batch> 14 <Area>山东</Area> 15 <AreaCode>SD</AreaCode> 16 <PlanCode>1000</PlanCode> 17 </LineEqui>
- 调用结果
上一篇: 中国古镇旅游景点大全 中国古镇有哪些地方
下一篇: 明年千万别买这6样东西:买了就后悔!