HttpClient example

example:

package com.fengshu.limanman;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;


public class SimpleClient {
	public static void main(String[] args) throws IOException {
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod("http://192.168.8.141:9080/Service/ws/ent/brand/info?entId=00000716&shopId=300014");
		// if post
		/*HttpMethodParams params = new HttpMethodParams();
		params.setParameter("entId", "00000716");
		params.setParameter("shopId", "300014");
		method.setParams(params);*/
		 //设置http头  
		method.addRequestHeader("Content-Type", "application/json");
		// 使用POST方法
		// HttpMethod method = new PostMethod("http://java.sun.com");
		client.executeMethod(method);
		// 打印服务器返回的状态
		System.out.println(method.getStatusLine());
		// 打印返回的信息
		System.out.println(method.getResponseBodyAsString());
		// 释放连接
		method.releaseConnection();
	}
}


设置连接超时的方法:


			// 连接超时设置
			client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
			// 请求超时
			client.getHttpConnectionManager().getParams().setSoTimeout(5000);


设置编码:

method.getParams().setContentCharset("utf-8"); 





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