HttpClient入门

这是get方式的情况:
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class TestHttpClient {
	public static void main(String[] args) {
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		String http = "";
		HttpGet httppost = new HttpGet(http);
		HttpResponse response;
		try {
			response = httpclient.execute(httppost);
			if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
				HttpEntity enties = response.getEntity();
				if (enties != null) {
					String str = EntityUtils.toString(enties);
				}

				if (enties != null) {
					enties.consumeContent();
				}
			}

		} catch (Exception e) {

		}finally{
                     httpclient.getConnectionManager().shutdown();  
                }
	}

}


POST的情况:
 List formparams = new ArrayList();
		formparams.add(new BasicNameValuePair("user_password", ""));
		UrlEncodedFormEntity entity2 = new UrlEncodedFormEntity(formparams,
				"UTF-8");
		httppost.setEntity(entity2);


文件类型使用FileBody,字符串类型使用StringBody,其它的还有ByteArrayBody,InputStreamBody.
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class TestHttpClient {
	public static void main(String[] args) {
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		String http = "http://。。。。.json";
		HttpPost httppost = new HttpPost(http);
		
		HttpResponse response;
		try {
			List formparams = new ArrayList();  
	        formparams.add(new BasicNameValuePair("email", "[email protected]"));  
	        formparams.add(new BasicNameValuePair("password", "123456"));  
	        formparams.add(new BasicNameValuePair("appName", "ferjek"));  
	        formparams.add(new BasicNameValuePair("url", "www.dkskeefl.com"));  
	        UrlEncodedFormEntity entity2 = new UrlEncodedFormEntity(formparams,  
	                "UTF-8");  
	        httppost.setEntity(entity2);  
			response = httpclient.execute(httppost);
			if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
				HttpEntity enties = response.getEntity();
				if (enties != null) {
					String str = EntityUtils.toString(enties);
					System.out.println("返回信息:"+str);
				}

				if (enties != null) {
					enties.consumeContent();
				}
			}

		} catch (Exception e) {

		} finally {
			httpclient.getConnectionManager().shutdown();
		}
	}

}



你可能感兴趣的:(ORACLE,WebService)