123 lines
5.3 KiB
C#
123 lines
5.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Web;
|
||
using System.Web.UI;
|
||
using System.Web.UI.WebControls;
|
||
using System.Xml;
|
||
|
||
namespace ApplyTest
|
||
{
|
||
public partial class SendPost : System.Web.UI.Page
|
||
{
|
||
/*addn_inf=测试&auth_code=282733289026142825&curr_type=CNY&goods_des=统一爱夸水&
|
||
goods_detail=测试&goods_tag=&ins_cd=08M0062887&mchnt_cd=0002900F0370561&
|
||
mchnt_order_no=100162980022116110115294700016&order_amt=300&order_type=ALIPAY&
|
||
random_str=6WS0PM&sence=1&term_id=10016298&term_ip=120.27.128.115&txn_begin_ts=20161101152947&version=1*/
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
////声明一个XMLDoc文档对象,LOAD()xml字符串
|
||
//XmlDocument doc = new XmlDocument();
|
||
//doc.LoadXml("<entity><version>1.2.0_2012_12_05</version></entity>");
|
||
////把XML发送出去
|
||
//Response.Write(doc.InnerXml);
|
||
//Response.End();
|
||
XmlDocument xmlDoc = new XmlDocument();
|
||
xmlDoc.Load("E:\\000_通用版本\\000_通用版本\\011_支付宝\\AppForApply\\ApplyTest\\XMLFile.xml");
|
||
returntext.Text = PostXml("http://116.239.4.195:28164/micropay?req=" +
|
||
HttpUtility.UrlEncode(ConvertXmlToString(xmlDoc), Encoding.GetEncoding("gbk")),
|
||
HttpUtility.UrlEncode(ConvertXmlToString(xmlDoc), Encoding.GetEncoding("gbk")));
|
||
//returntext.Text = PostXmlToUrl("http://116.239.4.195:28164/micropay?req=",
|
||
// System.Web.HttpUtility.UrlEncode(ConvertXmlToString(xmlDoc), Encoding.GetEncoding("gbk")));
|
||
DeCodeText.Text = System.Web.HttpUtility.UrlDecode(returntext.Text, Encoding.GetEncoding("gbk"));
|
||
}
|
||
|
||
/// C# POST 发送XML
|
||
/// </summary>
|
||
/// <param name="url">目标Url</param>
|
||
/// <param name="strPost">要Post的字符串(数据)</param>
|
||
/// <returns>服务器响应</returns>
|
||
private string PostXml(string url, string strPost)
|
||
{
|
||
string result = string.Empty;
|
||
//就是把字符串str按照简体中文(ASCIIEncoding.ASCII)的编码方式,
|
||
//编码成 Bytes类型的字节流数组;
|
||
// 要注意的这是这个编码方式,还有内容的Xml内容的编码方式,如果没有注意对应会出现文末的错误
|
||
byte[] buffer = Encoding.GetEncoding("gbk").GetBytes(strPost);
|
||
StreamWriter myWriter = null;
|
||
//根据url创建HttpWebRequest对象
|
||
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
|
||
//Our method is post, otherwise the buffer (postvars) would be useless
|
||
objRequest.Method = "POST";
|
||
objRequest.ContentLength = buffer.Length;
|
||
objRequest.ContentType = "text/xml";//提交xml
|
||
//objRequest.ContentType = "application/x-www-form-urlencoded";//提交表单
|
||
try
|
||
{
|
||
myWriter = new StreamWriter(objRequest.GetRequestStream(), Encoding.GetEncoding("gbk"));
|
||
myWriter.Write(strPost);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
return e.Message;
|
||
}
|
||
finally
|
||
{
|
||
myWriter.Close();
|
||
}
|
||
//读取服务器返回信息
|
||
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
|
||
//using作为语句,用于定义一个范围,在此范围的末尾将释放对象
|
||
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream(), Encoding.GetEncoding("gbk")))
|
||
{
|
||
//ReadToEnd适用于小文件的读取,一次性的返回整个文件
|
||
result = sr.ReadToEnd();
|
||
sr.Close();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
protected string PostXmlToUrl(string url, string data)
|
||
{
|
||
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
|
||
hwr.Method = "POST";
|
||
hwr.ContentType = "text/xml";
|
||
Stream stream = hwr.GetRequestStream();
|
||
|
||
StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.GetEncoding("gbk"));
|
||
sw.Write(data);
|
||
sw.Close();
|
||
|
||
stream = hwr.GetResponse().GetResponseStream();
|
||
|
||
StreamReader sr = new StreamReader(stream, System.Text.Encoding.GetEncoding("gbk"));
|
||
string ret = sr.ReadToEnd();
|
||
sr.Close();
|
||
|
||
return ret;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将XmlDocument转化为string
|
||
/// </summary>
|
||
/// <param name="xmlDoc"></param>
|
||
/// <returns></returns>
|
||
public string ConvertXmlToString(XmlDocument xmlDoc)
|
||
{
|
||
MemoryStream stream = new MemoryStream();
|
||
XmlTextWriter writer = new XmlTextWriter(stream, null);
|
||
writer.Formatting = Formatting.Indented;
|
||
xmlDoc.Save(writer);
|
||
StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
|
||
stream.Position = 0;
|
||
string xmlString = sr.ReadToEnd();
|
||
sr.Close();
|
||
stream.Close();
|
||
return xmlString;
|
||
}
|
||
}
|
||
} |