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

  • 相关阅读:
    iOS开发中多线程断点下载大文件
    iOS开发中的压缩以及解压
    iOS中大文件下载(单线程下载)
    NSURLConnection的GetPost方法
    自定义NSOperation下载图片
    NSOperation和NSOperationQueue的一些基本操作
    虚函数、继承
    new、delete、以及queue类
    new、delete、以及queue类
    在构造函数中使用new时的注意事项
  • 原文地址:https://www.cnblogs.com/emojio/p/10219360.html
Copyright © 2011-2022 走看看