zoukankan      html  css  js  c++  java
  • Java httpclient请求,解决乱码问题

    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));
        }
    
    }
  • 相关阅读:
    [转]Request Control Introduce
    [转]How to set the control word of FPU in delphi
    Delphi消息分发机制
    Delphi Handle Exception
    python 简单图像处理(13) 二值图腐蚀和膨胀,开运算、闭运算
    如何在Linux下实现50万并发
    转载 google hack
    Linux 网卡如何支持TSO GSO指南
    收藏:网口协商
    AVR地址空间
  • 原文地址:https://www.cnblogs.com/taoweiji/p/3746855.html
Copyright © 2011-2022 走看看