HttpClient在项目中的重要代码

package com.**.httpGet;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;


import com.**.start.GetResource;

public class HttpClientDeal {
	
	public boolean login(String login_conditions , String session_id){
		HttpClient httpClient = new HttpClient();
		String login_url = "";
		String check_login_str = "";
		login_url = GetResource.getXmlValue("login_url");
		check_login_str = GetResource.getXmlValue("check_login_str");
		login_url = login_url + "?" + login_conditions;
		GetMethod getMethod = new GetMethod(login_url);
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		int statusCode;
		String check_str;
		boolean check = false;
		try {
			statusCode = httpClient.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: "
						+ getMethod.getStatusLine());
			}			
			HttpClientMaintain.setClinet(session_id, httpClient);
			check_str = new String(getMethod.getResponseBody() , "UTF-8");
			if(check_str.indexOf(check_login_str) > -1){
				check = true;
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			getMethod.releaseConnection();
		}
		return check;
	}
	
	public String getMethod(String session_id , String url){
		String  str = null;
		HttpClient httpClient = HttpClientMaintain.getClinet(session_id);
		GetMethod getMethod = new GetMethod(url);
		int Debug = 0;
		String debug_path = "";
		int statusCode;
		StringBuffer  sb = null;
		try {
			statusCode = httpClient.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: "
						+ getMethod.getStatusLine());
			}
			byte[] bytes = getMethod.getResponseBody();
			Debug = Integer.parseInt(GetResource.getXmlValue("debug"));
			debug_path = GetResource.getXmlValue("debug_path");
			if(Debug == 1){
				DataOutputStream out =
				    new DataOutputStream (
				    new BufferedOutputStream (
				    new FileOutputStream (debug_path, true)));
				out.write(bytes);
				out.flush();
			}
			sb = new StringBuffer(new String(bytes , "GBK"));
			BufferedReader bf = new BufferedReader(new StringReader(sb
					.toString()));
			String line = "";

			while ((line = bf.readLine()) != null) {
				str = str + line;
			}
//			System.out.println("返回的页面"+str);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			getMethod.releaseConnection();
		}
		return str;
	}
	
	/**
	 * 
	 * @param session_id
	 * @param file_download_url:OA文件的下载地址
	 * @param file_name:下载后的文件名
	 */
	public void downloadMethod(String session_id , String file_download_url , String file_name){
		HttpClient httpClient = HttpClientMaintain.getClinet(session_id);
		GetMethod getMethod = new GetMethod(file_download_url);
		int statusCode;
		try {
			statusCode = httpClient.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: "
						+ getMethod.getStatusLine());
			}
			byte[] bytes = getMethod.getResponseBody();
			OutputStream serverout = new FileOutputStream(file_name);
	        serverout.write(bytes);   
	        serverout.flush();   
	        serverout.close(); 
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			getMethod.releaseConnection();
		}
	}
	

	public void postMethod(String session_id , String url , HashMap<String, String> map , ArrayList<String> list){
		HttpClient httpClient = HttpClientMaintain.getClinet(session_id);
		PostMethod postMethod = new PostMethod(url);
		NameValuePair[] data = new NameValuePair[list.size()];
		for(int i=0; i<list.size(); i++){
			String name = (String)list.get(i);
			String value = (String)map.get(name);
			data[i] = new NameValuePair(name , value);
		}
		postMethod.setRequestBody(data);
		int statusCode = 0;
		try {
			statusCode = httpClient.executeMethod(postMethod);
			if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
				Header locationHeader = postMethod.getResponseHeader("location");
				String location = null;
				if(locationHeader != null){
					location = locationHeader.getValue();
					System.out.println("the page is redirected to " + location);
				}else{
					System.out.println("Location field is null");
				}
				return;
			}else{
				System.out.println(postMethod.getStatusLine());
				String str = "";
				str = postMethod.getResponseBodyAsString();
				System.out.println(str);
			}
			postMethod.releaseConnection();
			return;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void postMethod(String session_id , String url ,  ArrayList<PutParam> list){
		HttpClient httpClient = HttpClientMaintain.getClinet(session_id);
		PostMethod postMethod = new PostMethod(url);
		NameValuePair[] data = new NameValuePair[list.size()];
		for(int i=0; i<list.size(); i++){
			PutParam model = (PutParam)list.get(i);
			String name = model.getParam();
			String value = model.getValue();
			data[i] = new NameValuePair(name , value);
//			System.out.println(name+"   "+value);
		}
		postMethod.setRequestBody(data);
		int statusCode = 0;
		try {
			statusCode = httpClient.executeMethod(postMethod);
			if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
				Header locationHeader = postMethod.getResponseHeader("location");
				String location = null;
				if(locationHeader != null){
					location = locationHeader.getValue();
					System.out.println("the page is redirected to " + location);
				}else{
					System.out.println("Location field is null");
				}
				return;
			}else{
				System.out.println(postMethod.getStatusLine());
				String str = "";
				str = postMethod.getResponseBodyAsString();
				System.out.println(str);
			}
			postMethod.releaseConnection();
			return;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 

你可能感兴趣的:(apache,windows)