少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

package com.abin.lee.junit.httpasyncclient.service;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpAsyncClientService extends HttpServlet{
 private static final long serialVersionUID = 807336917776643578L;

 @SuppressWarnings("rawtypes")
 public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{
  Map map=request.getParameterMap();
  String id=(String)((Object[])map.get("id"))[0].toString();
  if(null!=id&&!"".equals(id)){
   String result=id+" is response";
   System.out.println("result="+result);
   ServletOutputStream out=response.getOutputStream();
   BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
   writer.write(result);
   writer.flush();
   writer.close();
  }else{
   String result=id+" is null";
   System.out.println("result="+result);
   ServletOutputStream out=response.getOutputStream();
   BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
   writer.write(result);
   writer.flush();
   writer.close();
  }
 }
}








<servlet>
  <servlet-name>HttpAsyncClientService</servlet-name>
  <servlet-class>com.abin.lee.junit.httpasyncclient.service.HttpAsyncClientService</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>HttpAsyncClientService</servlet-name>
  <url-pattern>/HttpAsyncClientService</url-pattern>
 </servlet-mapping>





package com.abin.lee.junit.httpasyncclient.example;

import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.util.EntityUtils;

public class CreateHttpAsyncClient {
 public static void main(String[] args) throws Exception {
        HttpAsyncClient httpclient = new DefaultHttpAsyncClient();
        httpclient.start();
        try {
            HttpGet request = new HttpGet("http://localhost:7000/global/HttpAsyncClientService?id=5");
            Future<HttpResponse> future = httpclient.execute(request, null);
            HttpResponse response = future.get();
            System.out.println("Response: " + response.getStatusLine());
            System.out.println("Response1: " + EntityUtils.toString(response.getEntity()));
            System.out.println("Shutting down");
        } finally {
            httpclient.shutdown();
        }
        System.out.println("Done");
    }

}

posted on 2013-01-08 23:15 abin 阅读(5106) 评论(0)  编辑  收藏 所属分类: httpClient

只有注册用户登录后才能发表评论。


网站导航: