HttpWebRequest,HttpWebResponse C# 代码调用webservice,参数为xml

时间:2023-03-09 15:08:35
HttpWebRequest,HttpWebResponse C# 代码调用webservice,参数为xml
  1. 先上调用代码
             public static string PostMoths(string url, string Json)
    {
    System.Net.HttpWebRequest request;
    Stream writer;
    System.Net.HttpWebResponse response; try
    {
    string strURL = url;
    System.GC.Collect();
    System.Net.ServicePointManager.Expect100Continue = false;
    request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
    System.Net.ServicePointManager.DefaultConnectionLimit = ;
    request.ServicePoint.ConnectionLimit = ;
    request.KeepAlive = false;
    request.Timeout = * * ;
    request.Method = "POST";
    request.ProtocolVersion = HttpVersion.Version11;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
    request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
    string paraUrlCoded = Json;
    byte[] payload;
    payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
    request.ContentLength = payload.Length;
    writer = request.GetRequestStream();
    writer.Write(payload, , payload.Length); response = (System.Net.HttpWebResponse)request.GetResponse();
    System.IO.Stream s;
    s = response.GetResponseStream();
    string strValue = "";
    StreamReader Reader = new StreamReader(s, Encoding.UTF8);
    strValue = Reader.ReadToEnd();
    if (request != null)
    {
    request.Abort();
    request = null;
    }
    if (response != null)
    {
    response.Close();
    response = null;
    }
    if (writer != null)
    {
    writer.Close();
    writer = null;
    }
    return strValue;
    }
    catch (Exception ex)
    {
    var obj = new
    {
    status = ,
    msg = ex.Message
    };
    request = null;
    response = null;
    writer = null;
    return ex.Message;
    }
    finally
    {
    request = null;
    response = null;
    writer = null;
    }
    }
  2. 配置文件信息 WebConfig
     <system.web>
    <!--解决本地调用成功,外网调用失败的问题-->
    <webServices>
    <protocols>
    <add name="HttpSoap"/>
    <add name="HttpPost"/>
    <add name="HttpGet"/>
    <add name="Documentation"/>
    </protocols>
    </webServices>
    <!--上传文件大小限制-->
    <httpRuntime executionTimeout="" maxRequestLength="" useFullyQualifiedRedirectUrl="false" requestValidationMode="2.0"/>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" validateRequest="false"/>
    </system.web>

    配置文件中红色标注是必填项。

  3. webservice接口代码
         [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
    [System.Web.Script.Services.ScriptService]
    public class MESService : System.Web.Services.WebService
    {
    [WebMethod(Description = "")]
    public void GetData(string strXML)
    {
    //你的结果处理
    #region 处理数据 #endregion
    Context.Response.Write(Common.JsonHelper.SerializeToJson(new { status = , msg = strXML }));
    Context.Response.End();
    }
    }
  4. 接口参数
     <?xml version="1.0" encoding="UTF-8"?>
    <LineEqui
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <LineCode>W1</LineCode>
    <EquiCode>qwe</EquiCode>
    <PlanStatus>Running</PlanStatus>
    <DataTime>2018-02-02 09:34:16</DataTime>
    <FactoryCode></FactoryCode>
    <ProductCode></ProductCode>
    <PlanCount></PlanCount>
    <Unit>PC</Unit>
    <Batch></Batch>
    <Area>山东</Area>
    <AreaCode>SD</AreaCode>
    <PlanCode></PlanCode>
    </LineEqui>
  5. 调用结果

    HttpWebRequest,HttpWebResponse C# 代码调用webservice,参数为xml