HttpWebRequest,HttpWebResponse C# 代碼調用webservice,參數為xml
阿新 • • 發佈:2018-02-02
batch 信息 pos tle binding har comm 配置 ebs
- 先上調用代碼
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;
- 配置文件信息 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>
- 調用結果
HttpWebRequest,HttpWebResponse C# 代碼調用webservice,參數為xml