码迷,mamicode.com
首页 > 其他好文 > 详细

调用restFul接口

时间:2015-08-13 19:40:04      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

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(); } } }

 

调用restFul接口

标签:

原文地址:http://www.cnblogs.com/nyist2007/p/4728137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!