- call the code first
1 Public Static stringPostmoths (stringUrlstringJson)2 {3 System.Net.HttpWebRequest request;4 Stream writer;5 System.Net.HttpWebResponse response;6 7 Try8 {9 stringstrURL =URL;Ten System.GC.Collect (); OneSystem.Net.ServicePointManager.Expect100Continue =false; ARequest =(System.Net.HttpWebRequest) webrequest.create (strURL); -System.Net.ServicePointManager.DefaultConnectionLimit =1024x768; -Request. Servicepoint.connectionlimit = $; theRequest. KeepAlive =false; -Request. Timeout = +* -*5; -Request. Method ="POST"; -Request. ProtocolVersion =httpversion.version11; +Servicepointmanager.securityprotocol =Securityprotocoltype.tls; - request. ContentType = "Application/x-www-form-urlencoded;charset=utf-8"; + stringparaurlcoded =Json; A byte[] payload; atPayload =System.Text.Encoding.UTF8.GetBytes (paraurlcoded); -Request. ContentLength =payload. Length; -writer =request. GetRequestStream (); -Writer. Write (Payload,0, payload. Length); - -Response =(System.Net.HttpWebResponse) request. GetResponse (); in System.IO.Stream S; -s =Response. GetResponseStream (); to stringstrvalue =""; +StreamReader Reader =NewStreamReader (S, Encoding.UTF8); -strvalue =reader.readtoend (); the if(Request! =NULL) * { $ request. Abort ();Panax NotoginsengRequest =NULL; - } the if(Response! =NULL) + { A Response. Close (); theResponse =NULL; + } - if(Writer! =NULL) $ { $ writer. Close (); -writer =NULL; - } the returnstrvalue; - }Wuyi Catch(Exception ex) the { - varobj =New Wu { -Status =0, Aboutmsg =Ex. Message $ }; -Request =NULL; -Response =NULL; -writer =NULL; A returnEx. Message; + } the finally - { $Request =NULL; theResponse =NULL; thewriter =NULL; the } the}
- configuration file Information Webconfig
1<system.web>2<!--solve the problem of local call success, external network call failed--3<webServices>4<protocols>5<add name="HttpSoap"/>6<add name="HttpPost"/>7<add name="HttpGet"/>8<add name="Documentation"/>9</protocols>Ten</webServices> One<!--upload file size limit-- A" -"Maxrequestlength="100960"Usefullyqualifiedredirecturl="false" requestvalidationmode= "2.0"/> -<pages controlrenderingcompatibilityversion="3.5"Clientidmode="autoid" validaterequest= "false"/> -</system.web>
A red callout in the configuration file is required.
- WebService Interface Code
1[WebService (Namespace ="http://tempuri.org/")]2[WebServiceBinding (ConformsTo =wsiprofiles.basicprofile1_1)]3 //[System.ComponentModel.ToolboxItem (false)]4 //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. 5 [System.Web.Script.Services.ScriptService]6 Public classMESService:System.Web.Services.WebService7 {8[WebMethod (Description ="")]9 Public voidGetData (stringstrxml)Ten { One //your results to deal with A #regionWorking with Data - - #endregion theContext.Response.Write (Common.JsonHelper.SerializeToJson (New{status =111, msg =strxml})); - Context.Response.End (); - } -}
- Interface Parameters
1<?xml version="1.0"encoding="UTF-8"?>2<Lineequi3Xmlns:xsd="Http://www.w3.org/2001/XMLSchema"4Xmlns: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>Ten<ProductCode>444</ProductCode> One<PlanCount>10000</PlanCount> A<Unit>PC</Unit> -<Batch>123</Batch> -<Area> Shandong </Area> the<AreaCode>SD</AreaCode> -<PlanCode> +</PlanCode> -</LineEqui>
- Call result
Httpwebrequest,httpwebresponse C # code calls WebService, parameter is XML