码迷,mamicode.com
首页 > Web开发 > 详细

httpClient模拟登陆校内某系统

时间:2017-05-28 23:16:33      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:状态   系统   stack   数据   exec   name   final   请求   for   

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模拟登陆校内某系统

标签:状态   系统   stack   数据   exec   name   final   请求   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!