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

2021-02-16 20:17

阅读:538

标签:sage   结果   serial   red   stl   toe   rect   分享图片   limit   

  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  2     
     3      4        5         "HttpSoap"/>
     6         "HttpPost"/>
     7         "HttpGet"/>
     8         "Documentation"/>
     9       10     11     
    12     "300" maxRequestLength="100960" useFullyQualifiedRedirectUrl="false" requestValidationMode="2.0"/>
    13     "3.5" clientIDMode="AutoID" validateRequest="false"/>
    14 

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

  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 "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     W1 6     qwe 7     Running 8     2018-02-02 09:34:16 9     222210     44411     1000012     PC13     12314     山东
    15     SD
    16     100017 
  5. 调用结果

    技术分享图片

 

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

标签:sage   结果   serial   red   stl   toe   rect   分享图片   limit   

原文地址:https://www.cnblogs.com/yuchenlove/p/8403579.html


评论


亲,登录后才可以留言!