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修改即可

  • 相关阅读:
    Volatile的作用---http://www.cnblogs.com/xing901022/p/7840684.html
    基于JDBC持久化的事务管理-https://www.cnblogs.com/xing901022/p/4272420.html
    Class的isAssignableFrom方法--其他博主的博客
    深入并发二 ThreadLocal源码与内存泄漏相关分析 https://www.cnblogs.com/qmlingxin/p/9412061.html
    Beta阶段项目总结
    Alpha阶段项目总结
    Alpha版总结会议——班级派
    第二冲刺阶段——站立会议第十四天6月7日
    第二冲刺阶段——站立会议第十三天6月6日
    第二冲刺阶段——站立会议第十二天6月5日
  • 原文地址:https://www.cnblogs.com/emojio/p/10219360.html
Copyright © 2011-2022 走看看