ITeye博客

HttpClint_jar

select*from爱 · 2011-04-15 17:18 · 资料下载 · Markdown

如题,见附件
commons-codec-1.3.jar commons-httpclient-3.1-alpha1.jar commons-logging-1.1.1.jar


package dj.uri;
import java.io.IOException;
import java.net.*;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;


public class Test {

	/**
	 * @param args
	 * @throws URISyntaxException 
	 * @throws IOException 
	 * @throws HttpException 
	 */
	public static void main(String[] args) throws URISyntaxException, HttpException, IOException {
	
		HttpClient client = new HttpClient(); 
		GetMethod getMethod = new GetMethod("http://www.baidu.com/");

		//设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
		    		new DefaultHttpMethodRetryHandler()); 
		System.out.println(getMethod.getResponseCharSet());
		//执行getMethod
		int statusCode = client.executeMethod(getMethod);
		if (statusCode != HttpStatus.SC_OK) {
		  System.err.println("Method failed: " + getMethod.getStatusLine());
		}
		
		byte[] responseBody = getMethod.getResponseBody();
		
System.out.println(new String(responseBody,getMethod.getResponseCharSet()));
		getMethod.releaseConnection();

	}

}



评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。