读书人

初学者问个接口有关问题

发布时间: 2014-01-23 14:48:04 作者: rapoo

菜鸟问个接口问题
求大神们给个详细的回复

我想做一个ashx的接口来接收别人post过来的json数据,怎么写?
最好就是连别人是怎么过来post过来的例子都有,我想看看是怎么交互的!!
[解决办法]

引用:
Quote: 引用:

求大神们给个详细的回复

我想做一个ashx的接口来接收别人post过来的json数据,怎么写?
最好就是连别人是怎么过来post过来的例子都有,我想看看是怎么交互的!!


那如果我不用url传呢?


可以啊,稍微改下就行了。
接收数据:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
//string JsonData = context.Request.QueryString["Json"];
string JsonData = context.Request["Json"];
context.Response.ContentType = "text/plain";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
//context.Response.Write("Hello World");
context.Response.Write(string.Format("你刚刚提交上来的数据:\n{0}", JsonData));
}



public bool IsReusable
{
get
{
return false;
}
}
}
}


提交数据:
            System.Collections.Specialized.NameValueCollection Data = new System.Collections.Specialized.NameValueCollection();
Data.Add("Json", "{\"Name\":\"Tom\",\"Age\":\"20\"}\"");
MessageBox.Show(Encoding.UTF8.GetString(new System.Net.WebClient().UploadValues("http://localhost:7240/Handler1.ashx", "POST", Data)));

读书人网 >asp.net

热点推荐