首页 > Web开发 > 详细

httpClient模拟登陆校内某系统

时间:2017-05-28 22:53:26      阅读:348      评论:0      收藏:0      [点我收藏+]
package com.huowolf;

import java.util.ArrayList;
import java.util.List; 

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class Demo1 {
	private static CloseableHttpClient httpclient = HttpClients.createDefault();  
	
	public static void main(String[] args) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("loginName", "2013023024"));
		params.add(new BasicNameValuePair("password", "2013023024"));
		
		String url = "http://172.16.51.37/";
		post(url, params);
	}

	private static void post(String url,List<NameValuePair> params) {
		String body = null;
		HttpPost httpPost = null;
		CloseableHttpResponse response = null;
		try {
			httpPost = new HttpPost(url);
			httpPost.setEntity(new UrlEncodedFormEntity(params));	//设置參数
			response = httpclient.execute(httpPost); 				//发送请求
			System.out.println(response.getStatusLine());			//打印响应状态
			
			HttpEntity entity = response.getEntity();			 	//获取返回数据
			body = EntityUtils.toString(entity);
			if(entity!=null)
			{
				System.out.println(body);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				httpclient.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
	}

	
}

httpClient模拟登陆校内某系统

原文:http://www.cnblogs.com/jzssuanfa/p/6916811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!