zoukankan      html  css  js  c++  java
  • 发送http请求

    //统一编码格式'UTF-8'
        private static String ENCODING = "UTF-8";
    
        public static String GET(String url, Map<String, String> paramsMap) {
            try {
                HttpClient client = new DefaultHttpClient();
                //发送get请求
                HttpGet request = new HttpGet(url + StringUtils.convertParam(paramsMap ,ENCODING));
                HttpResponse response = client.execute(request);
    
                /**请求发送成功,并得到响应**/
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    /**读取服务器返回过来的json字符串数据**/
                    String strResult = EntityUtils.toString(response.getEntity());
                    return strResult;
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
    
        public static String POST(String url, Map<String, String> paramsMap) {
            CloseableHttpClient client = HttpClients.createDefault();
            String responseText = "";
            CloseableHttpResponse response = null;
            try {
                HttpPost method = new HttpPost(url);
                if (paramsMap != null) {
                    List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                    for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                        NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                        paramList.add(pair);
                    }
                    method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
                }
                response = client.execute(method);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    responseText = EntityUtils.toString(entity);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return responseText;
        }
    
        public static String postXml(String url, String param)  throws Exception {
            HttpPost post = null;
            try {
                HttpClient httpClient = new DefaultHttpClient();
    
                // 设置超时时间
                httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
                httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
    
                post = new HttpPost(url);
                // 构造消息头
                post.setHeader("Content-type", "text/xml; charset=utf-8");
                post.setHeader("Connection", "Close");
    
                // 构建消息实体
                StringEntity entity = new StringEntity(param, "UTF-8");
                entity.setContentEncoding("UTF-8");
                // 发送Json格式的数据请求
                entity.setContentType("text/xml");
                post.setEntity(entity);
    
                HttpResponse response = httpClient.execute(post);
    
                // 检验返回码
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    return EntityUtils.toString(response.getEntity(),"utf-8");
    
                } else {
                    //404...
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }

    如果是其他请求将HTTPPost修改即可

  • 相关阅读:
    poj 1013 Counterfeit Dollar
    poj百练2973:Skew数 进制问题
    poj百练2972 进制问题
    poj2080 Calendar
    POJ 1928 The Peanuts
    EXCEL打开CSV文件乱码的解决方法
    希望博客园做个软件职业生存状态调查问卷
    Linq使用Group By [转]
    ADO.NET 从DataTable中获取某列含有的不同值的几种方式
    本该遭拒的十大科技专利:苹果滑动解锁上榜[转]
  • 原文地址:https://www.cnblogs.com/emojio/p/10219360.html
Copyright © 2011-2022 走看看