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();

    }

  • 相关阅读:
    【问题:SSH】win10使用SSH链接服务器时,提示:Host key verification failed
    【深度学习大讲堂】首期第三讲:深度学习基础 第一部分:基础结构单元
    【深度学习大讲堂】首期第二讲:深度学习简史
    【深度学习大讲堂】首期第一讲:人工智能的ABCDE 第二部分:简谈当前AI技术与发展趋势
    【深度学习大讲堂】首期第一讲:人工智能的ABCDE 第一部分:从人工智能和计算机视觉说起
    实验吧-隐写-so beautiful so white WP
    实验吧-隐写 最低位的亲吻 WP
    jarvis OJ basic (一) WP
    实验吧web-登入一下好吗
    实验吧web-oncemore
  • 原文地址:https://www.cnblogs.com/yyangjing/p/11467037.html
Copyright © 2011-2022 走看看