Ajax的get/post请求服务器响应

 

Ajax的get/post方式http请求代码

1.服务器端响应AJAX的同步请求,添加一个类继承IhttpHandler接口

public class server:IHttpHandler

{

       public server()

       {     }

 

    #region IHttpHandler 成员

 

    bool IHttpHandler.IsReusable

    {

        get { return true; }

    }

 

    void IHttpHandler.ProcessRequest(HttpContext context)

{

     //获取get请求方式提交的cardid参数

         string id= context.Request["cardid"];

         //设置客户端不缓存,每次都请求服务器

         context.Response.CacheControl = "no-cache";

          //返回的客户端信息

         context.Response.Write(“测试通过!”);

 

        //post请求方式的处理

//Stream se= context.Request.InputStream;

      //  StreamReader streamreader = new StreamReader(se);

       // String  str = streamreader.ReadToEnd();

      //  se.Close();

      //  streamreader.Close();

      //  context.Response.Write(str);

      

    }

 

    #endregion

}

 

Web.config 还要配置如下

             

                    

             

 

2. 服务器端响应AJAX的异步请求,添加一个类继承IhttpAsyncHandler接口

public class server : IHttpAsyncHandler

{

    public server()

    {

        //

        //TODO: 在此处添加构造函数逻辑

        //

    }

 

    public bool IsReusable { get { return false; } }

 

 

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)

    {

        context.Response.Write("

Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "

\r\n");

        AsynchOperation asynch = new AsynchOperation(cb, context, extraData);

        asynch.StartAsyncWork();

        return asynch;

    }

 

    public void EndProcessRequest(IAsyncResult result)

    {

    }

 

    public void ProcessRequest(HttpContext context)

    {

        throw new InvalidOperationException();

    }

}

 

class AsynchOperation : IAsyncResult

{

    private bool _completed;

    private Object _state;

    private AsyncCallback _callback;

    private HttpContext _context;

 

    bool IAsyncResult.IsCompleted { get { return _completed; } }

    WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } }

    Object IAsyncResult.AsyncState { get { return _state; } }

    bool IAsyncResult.CompletedSynchronously { get { return false; } }

 

    public AsynchOperation(AsyncCallback callback, HttpContext context, Object state)

    {

        _callback = callback;

        _context = context;

        _state = state;

        _completed = false;

    }

 

    public void StartAsyncWork()

    {

        ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);

    }

 

    private void StartAsyncTask(Object workItemState)

    {

 

        _context.Response.Write("

Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "

\r\n");

 

        _context.Response.Write("Hello World from Async Handler!");

        _completed = true;

        _callback(this);

    }

}

 

 

 

参考文献:http://msdn.microsoft.com/zh-cn/library/system.web.httpcontext.aspx

http://msdn.microsoft.com/zh-cn/library/system.web.httpcontext.aspx

你可能感兴趣的:(Ajax的get/post请求服务器响应)