Httpwebrequest,httpwebresponse C # code calls WebService, parameter is XML

Source: Internet
Author: User

  1. 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}
  2. 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.

  3. 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 (); -         } -}
  4. 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>
  5. Call result

    

Httpwebrequest,httpwebresponse C # code calls WebService, parameter is XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.