1. 程式人生 > >HttpWebRequest,HttpWebResponse C# 代碼調用webservice,參數為xml

HttpWebRequest,HttpWebResponse C# 代碼調用webservice,參數為xml

batch 信息 pos tle binding har comm 配置 ebs

  1. 先上調用代碼
     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 }
  2. 配置文件信息 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>

    配置文件中紅色標註是必填項。

  3. 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     }
  4. 接口參數
     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>
  5. 調用結果

    技術分享圖片

HttpWebRequest,HttpWebResponse C# 代碼調用webservice,參數為xml