C#中怎么将XML作为参数post到接口

本文介绍了一种通过HTTP POST请求向服务器提交包含中文字符的XML数据的方法,并展示了如何使用C#来实现这一过程。文章提供了完整的代码示例,包括设置请求头、编码数据以及读取响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String xml = "<data>中文</data>";
String postData
= "data=" + Server.UrlEncode(xml); ;
string strUrl = "http://localhost:29833/WebSite1/xx.aspx";     
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Method
= "POST";      
myRequest.ContentType
="application/x-www-form-urlencoded";
myRequest.ContentLength
= postData.Length;
Stream newStream
=myRequest.GetRequestStream();     
byte[] data = Encoding.GetEncoding("GB2312").GetBytes(postData);
// 发送数据
newStream.Write(data,0,data.Length);
HttpWebResponse res
= myRequest.GetResponse() as HttpWebResponse;
StreamReader sr 
= new StreamReader(res.GetResponseStream(), Encoding.UTF8);
String ret
= sr.ReadToEnd();
newStream.Close();
Response.Write(
"你提交的是:" + Server.HtmlEncode( ret));


//==========

接口的接收
Response.ClearContent();
Response.Write(Request.Form["data"]);
Response.End();

 

 

from:http://topic.youkuaiyun.com/u/20101117/11/bfee4a29-d966-4870-9026-0243767067a6.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值