zoukankan      html  css  js  c++  java
  • .net post一个xml文件到url

    string webURL = "https://------/third-article/yimeikeji/addArticle";
    StringBuilder strBuilder = new StringBuilder();
    strBuilder.Append("<?xml version=" + @"""1.0""" + " encoding=" + @"""utf-8""" + "?>" + " ");
    strBuilder.Append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=" + @"""http://schemas.xmlsoap.org/soap/envelope/""" + " ");
    strBuilder.Append("xmlns:SOAP-ENC=" + @"""http://schemas.xmlsoap.org/soap/encoding/""" + " ");
    strBuilder.Append("xmlns:xsi=" + @"""http://www.w3.org/2001/XMLSchema-instance""" + " ");
    strBuilder.Append("xmlns:xsd=" + @"""http://www.w3.org/2001/XMLSchema""" + " " + "xmlns:Spider=" + @"""urn:http://newshoo.com""" + ">" + " ");
    strBuilder.Append("<SOAP-ENV:Body encodingStyle=" + @"""http://schemas.xmlsoap.org/soap/encoding/""" + ">" + " ");
    strBuilder.Append("<Spider:PostDocument>" + " ");
    strBuilder.Append("<document xsi:type=" + @"""Spider:Document""" + ">" + " ");
    strBuilder.Append("<channel xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + channel + "]]>");
    strBuilder.Append("</channel>" + " ");
    strBuilder.Append("<origin xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + origin + "]]>");
    strBuilder.Append("</origin>" + " ");
    strBuilder.Append("<title xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + title + "]]>");
    strBuilder.Append("</title>" + " ");
    strBuilder.Append("<url xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + url + "]]>");
    strBuilder.Append("</url>" + " ");
    strBuilder.Append("<datetime xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + datetime + "]]>");
    strBuilder.Append("</datetime>" + " ");
    strBuilder.Append("<category xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + category + "]]>");
    strBuilder.Append("</category>" + " ");
    strBuilder.Append("<origin-datetime xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + origindatetime + "]]>");
    strBuilder.Append("</origin-datetime>" + " ");
    for (int j = 0; j < filename.Count(); j++)
    {
    strBuilder.Append("<item xsi:type=" + @"""ns:File""" + ">" + " ");
    strBuilder.Append("<filename xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + filename[j].ToString() + "]]>");
    strBuilder.Append("</filename>" + " ");
    strBuilder.Append("<name xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("</name>" + " ");
    strBuilder.Append("<in xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + filename[j + 1].ToString() + "]]>");
    strBuilder.Append("</in>" + " ");
    strBuilder.Append("</item>" + " ");
    j = j + 1;
    }
    strBuilder.Append("<content xsi:type=" + @"""xsd:string""" + ">" + " ");
    strBuilder.Append("<![CDATA[" + CONTENT + "]]>");
    strBuilder.Append("</content>" + " ");
    strBuilder.Append("</document>" + " ");
    strBuilder.Append("</Spider:PostDocument>" + " ");
    strBuilder.Append("</SOAP-ENV:Body>" + " ");
    strBuilder.Append("</SOAP-ENV:Envelope> " + " ");
    string postData = strBuilder.ToString();
    var encoding = Encoding.GetEncoding("UTF-8");
    byte[] buffer = encoding.GetBytes(postData);
    //创建链接
    try
    {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webURL);
    request.Timeout = 20000;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
    request.Method = "post";
    request.KeepAlive = true;
    request.AllowAutoRedirect = true;
    request.ContentLength = buffer.Length;
    request.ContentType = "text/xml";
    Stream newStream = request.GetRequestStream();
    byte[] Lastdata = Encoding.GetEncoding("UTF-8").GetBytes(postData);
    newStream.Write(Lastdata, 0, Lastdata.Length);
    //开始请求,并得到返回值
    HttpWebResponse res = request.GetResponse() as HttpWebResponse;
    StreamReader srdere = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
    String ret = srdere.ReadToEnd();
    newStream.Close();
    JObject array;
    array = (JObject)JsonConvert.DeserializeObject(ret);
    //string msg = array["msg"].ToString().Trim();

    }

  • 相关阅读:
    静态文件
    orm多表操作
    thinkphp5.0模块设计
    thinkphp5.0URL访问
    thinkphp5.0入口文件
    thinkphp5.0生命周期
    thinkphp5.0架构总览
    thinkphp5.0目录结构
    thinkphp5.0开发规范
    thinkphp5.0安装
  • 原文地址:https://www.cnblogs.com/yyangjing/p/11467037.html
Copyright © 2011-2022 走看看