码迷,mamicode.com
首页 > 编程语言 > 详细

Java httpclient请求,解决乱码问题

时间:2014-05-26 12:17:34      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:android   style   c   class   blog   code   

bubuko.com,布布扣
public class HttpPostRequestUtil {

    public HttpPostRequestUtil() {

    }
    public static String post(String url, Map<String, String> maps) {
        // 第一步,创建HttpPost对象
        HttpPost httpPost = new HttpPost(url);

        // 设置HTTP POST请求参数必须用NameValuePair对象
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        if (params != null) {
            Set<String> keys = maps.keySet();
            for (String key : keys) {
                System.out.println(maps.get(key));
                params.add(new BasicNameValuePair(key, maps.get(key)));
                
            }
        }
        
//        params.add(new BasicNameValuePair("action", "downloadAndroidApp"));
//        params.add(new BasicNameValuePair("packageId",
//                "89dcb664-50a7-4bf2-aeed-49c08af6a58a"));
//        params.add(new BasicNameValuePair("uuid", "test_ok1"));

        HttpResponse httpResponse = null;
        try {
            // 设置httpPost请求参数
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            httpResponse = new DefaultHttpClient().execute(httpPost);
            // System.out.println(httpResponse.getStatusLine().getStatusCode());
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 第三步,使用getEntity方法活得返回结果
                String result = EntityUtils.toString(httpResponse.getEntity());
                System.out.println("result:" + result);
                return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    

    public static void main(String[] args) {
        
        System.out.println(post("http://user.qzone.qq.com/876187500", null));
    }

}
bubuko.com,布布扣

 

Java httpclient请求,解决乱码问题,布布扣,bubuko.com

Java httpclient请求,解决乱码问题

标签:android   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/taoweiji/p/3746855.html

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