.net post一个xml文件到url

string webURL = "https://------/third-article/yimeikeji/addArticle";
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("<?xml version=" + @"""1.0""" + " encoding=" + @"""utf-8""" + "?>" + "\n");
strBuilder.Append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=" + @"""http://schemas.xmlsoap.org/soap/envelope/""" + "\n");
strBuilder.Append("xmlns:SOAP-ENC=" + @"""http://schemas.xmlsoap.org/soap/encoding/""" + "\n");
strBuilder.Append("xmlns:xsi=" + @"""http://www.w3.org/2001/XMLSchema-instance""" + "\n");
strBuilder.Append("xmlns:xsd=" + @"""http://www.w3.org/2001/XMLSchema""" + "\n" + "xmlns:Spider=" + @"""urn:http://newshoo.com""" + ">" + "\n");
strBuilder.Append("<SOAP-ENV:Body encodingStyle=" + @"""http://schemas.xmlsoap.org/soap/encoding/""" + ">" + "\n");
strBuilder.Append("<Spider:PostDocument>" + "\n");
strBuilder.Append("<document xsi:type=" + @"""Spider:Document""" + ">" + "\n");
strBuilder.Append("<channel xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + channel + "]]>");
strBuilder.Append("</channel>" + "\n");
strBuilder.Append("<origin xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + origin + "]]>");
strBuilder.Append("</origin>" + "\n");
strBuilder.Append("<title xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + title + "]]>");
strBuilder.Append("</title>" + "\n");
strBuilder.Append("<url xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + url + "]]>");
strBuilder.Append("</url>" + "\n");
strBuilder.Append("<datetime xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + datetime + "]]>");
strBuilder.Append("</datetime>" + "\n");
strBuilder.Append("<category xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + category + "]]>");
strBuilder.Append("</category>" + "\n");
strBuilder.Append("<origin-datetime xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + origindatetime + "]]>");
strBuilder.Append("</origin-datetime>" + "\n");
for (int j = 0; j < filename.Count(); j++)
{
strBuilder.Append("<item xsi:type=" + @"""ns:File""" + ">" + "\n");
strBuilder.Append("<filename xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + filename[j].ToString() + "]]>");
strBuilder.Append("</filename>" + "\n");
strBuilder.Append("<name xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("</name>" + "\n");
strBuilder.Append("<in xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + filename[j + 1].ToString() + "]]>");
strBuilder.Append("</in>" + "\n");
strBuilder.Append("</item>" + "\n");
j = j + 1;
}
strBuilder.Append("<content xsi:type=" + @"""xsd:string""" + ">" + "\n");
strBuilder.Append("<![CDATA[" + CONTENT + "]]>");
strBuilder.Append("</content>" + "\n");
strBuilder.Append("</document>" + "\n");
strBuilder.Append("</Spider:PostDocument>" + "\n");
strBuilder.Append("</SOAP-ENV:Body>" + "\n");
strBuilder.Append("</SOAP-ENV:Envelope> " + "\n");
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();

}

posted @ 2019-09-05 16:41  yangjingblog  阅读(197)  评论(0编辑  收藏  举报