模拟浏览器登陆

static String loginIndexUrl = "http://127.0.0.1/xiangmuming";
public String getCookieVal(String url, String JSESSIONID) {
		String cookieVal = "";
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			// 发送get请求
			HttpGet request = new HttpGet(url);
			request.setHeader("Cookie", JSESSIONID);
			try {
				CloseableHttpResponse response = httpclient.execute(request);
				HttpEntity entity = null;
				try {
					Header[] headers = response.getAllHeaders();
					for (int i = 0; i < headers.length; i++) {
						// System.out.println(headers[i].getName() +"=="+ headers[i].getValue());
						if ("Set-Cookie".equals(headers[i].getName())) {
							cookieVal = headers[i].getValue().substring(0, headers[i].getValue().indexOf(";"));
							break;
						}
					}
				} finally {
					EntityUtils.consume(entity);
					response.close();
				}
			} finally {
				httpclient.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return cookieVal;
	}
public int doLogin(String userName, String password, String cookie) {
		int statusCode = 0;
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		try {
			HttpPost httpPost = new HttpPost(loginUrl);
			httpPost.setHeader("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
			httpPost.setHeader("Cookie", cookie);
			httpPost.setHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");

			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("usertype", "0"));
			nvps.add(new BasicNameValuePair("type", "login"));
			nvps.add(new BasicNameValuePair("username", userName));
			nvps.add(new BasicNameValuePair("password", password));
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			httpClient = HttpClients.createDefault();
			response = httpClient.execute(httpPost);
			statusCode = response.getStatusLine().getStatusCode();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return statusCode;
	}

	/**
	 * post请求(用于请求json格式的参数)
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	public String doUrlPost(String url, List<NameValuePair> nvps, String JSESSIONID) {
		CloseableHttpResponse response = null;
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);
			httpPost.setHeader("Accept", "text/plain, */*; q=0.01");
			httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			httpPost.setHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
			httpPost.setHeader("Cookie", JSESSIONID);

			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

			response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity);
				return jsonString;
			} else {
				System.out.println("请求返回:" + state + "(" + url + ")");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值