没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
8页
byte[] byts = new byte[Request.InputStream.Length]; Request.InputStream.Read(byts,0,byts.Length); string req = System.Text.Encoding.Default.GetString(byts); req = Server.UrlDecode(req);
资源推荐
资源详情
资源评论
ASP.NET POST XML JSON数据,发送与接收
接收端通过Request.InputStream读取:
byte[] byts = new byte[Request.InputStream.Length];
Request.InputStream.Read(byts,0,byts.Length);
string req = System.Text.Encoding.Default.GetString(byts);
req = Server.UrlDecode(req);
对于完整的XML数据,可以:
xmlDoc = new XmlDocument();
xmlDoc.load(Request.InputStream);
将数据作为XML数据发送,例如:
public void PostXml(string url, string xml)
{
byte[] bytes = Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
接收端通过Request.InputStream读取:
byte[] byts = new byte[Request.InputStream.Length];
Request.InputStream.Read(byts,0,byts.Length);
string req = System.Text.Encoding.Default.GetString(byts);
req = Server.UrlDecode(req);
对于完整的XML数据,可以:
xmlDoc = new XmlDocument();
xmlDoc.load(Request.InputStream);
将数据作为XML数据发送,例如:
public void PostXml(string url, string xml)
{
byte[] bytes = Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) {
string message = String.Format("POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}
------------------------------------
C# code
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/json";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
using (var reader = new System.IO.StreamReader(context.Request.InputStream))
{
String xmlData = reader.ReadToEnd();
if (!string.IsNullOrEmpty(xmlData))
{
//业务处理
}
}
}
public bool IsReusable {
string message = String.Format("POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}
------------------------------------
C# code
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/json";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
using (var reader = new System.IO.StreamReader(context.Request.InputStream))
{
String xmlData = reader.ReadToEnd();
if (!string.IsNullOrEmpty(xmlData))
{
//业务处理
}
}
}
public bool IsReusable {
剩余7页未读,继续阅读
资源评论
- learningfromyou2015-08-10学习学习还是不错的
lboobl
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功