标签:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; public class TestMethod { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://url.url");//接口地址 httpPost.setHeader("Cache-Control", "max-age=0"); httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpPost.setHeader("Accept-Encoding", "gzip,deflate,sdch"); httpPost.setHeader("Accept-Language", "en-US,en;q=0.8"); httpPost.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3"); httpPost.setHeader("Accept-Encoding", "gzip,deflate,sdch"); HttpResponse response; try { response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { java.io.InputStream instreams = entity.getContent(); BufferedReader bf = new BufferedReader(new InputStreamReader(instreams,"utf-8"));
// 文件处理方式 StringBuilder sb = new StringBuilder(); String line = null; while ((line = bf.readLine()) != null) { sb.append(line + "\n"); } System.out.println(sb.toString()); httpPost.abort(); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/nyist2007/p/4728137.html