java模拟http请求

项目路径图

http请求类

public class Utils {


public Utils() {
       // TODO Auto-generated constructor stub
   }
    
   /**
    * 
    * @param path
    * @param map
    * @param encode
    * @return
    */
    
   public static String sendHttpClientPost(String path,
           Map<String, String> map, String encode) {
       List<NameValuePair> list=new ArrayList<NameValuePair>();
       for(Map.Entry<String, String> entry:map.entrySet()){
            
           list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
            
       }
       try {
           //实现将请求中的参数封装到请求参数中,请求体中
           UrlEncodedFormEntity  entity=new UrlEncodedFormEntity(list,encode);
           //使用post方式提交
           HttpPost httpPost=new HttpPost(path);
           httpPost.setEntity(entity);
           //指定post方式提交数据
           DefaultHttpClient client =new DefaultHttpClient();
            
           HttpResponse httpResponse=client.execute(httpPost);
           if(httpResponse.getStatusLine().getStatusCode()==200){
               return changeInputStream(httpResponse.getEntity().getContent(),encode);
                
           }
            
       } catch (Exception e) {
           // TODO: handle exception
       }
        
        
       return "";
   }
   /**
    * 
    * @param inputStream
    * @param encode
    * @return
    */
   private static String changeInputStream(InputStream inputStream,
           String encode) {
       // TODO Auto-generated method stub
 
       ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
       int len = 0;
       byte[] date = new byte[1024];
       String result = "";
       try {
 
           while ((len = inputStream.read(date)) != -1) {
 
               outputStream.write(date, 0, len);
           }
           result = new String(outputStream.toByteArray(), encode);
            
           return result;
 
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
 
       return null;
   }
   public static void main(String[] args) {
       String path="http://localhost:7878/booway/LoginServlet";
       Map<String, String> params = new HashMap<String, String>();
       params.put("username", "admin");
       params.put("password", "123");
        
       String result=Utils.sendHttpClientPost(path, params, "utf-8");
       System.out.println(result);
        
   }
}

登录实验类

public class LoginServlet extends HttpServlet{


private static final long serialVersionUID = 3295018296137007316L;



@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}



@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
       response.setCharacterEncoding("utf-8");
       request.setCharacterEncoding("utf-8");
        
       PrintWriter out=response.getWriter();
        
       String username=request.getParameter("username");
       String password=request.getParameter("password");
       System.out.println("---->username:"+username);
       System.out.println("---->password:"+password);
        
       if(username.equals("admin")&&password.equals("123")){
           out.println("login is success ! ");
       }else{
           out.println("login is failure ! ");
            
       }
        
       out.flush();
       out.close();

}

@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值