栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java Post ,Get 调用Http Https

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java Post ,Get 调用Http Https

 
    public static String doPostJson(String url, String json) {
        // 创建Httpclient对象
        HttpClient httpClient = null;
        String resultString = "";
        try {
            httpClient = new SSLClient();
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 请求头
            httpPost.addHeader("x-tif-paasid", "tywg");
            httpPost.addHeader("x-tif-timestamp", null);
            httpPost.addHeader("x-tif-signature", null);
            httpPost.addHeader("x-tif-nonce", null);
            httpPost.addHeader("Cache-Control", "no-cache");
            // 执行http请求
            HttpResponse response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

 
    public static String doGet(String url, String charset){
        if(null == charset){
            charset = "utf-8";
        }
        HttpClient httpClient = null;
        HttpGet httpGet= null;
        String result = null;

        try {
            httpClient = new SSLClient();
            httpGet = new HttpGet(url);

            HttpResponse response = httpClient.execute(httpGet);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

 
       public static String doGets(String stoptime, String audittype, String ip, Integer istrust, Integer ipmacbind) {
        String url = "";
        String data = "";
        String result = "";
        StringBuffer sb = new StringBuffer(url);
        if (null != stoptime && !"".equals(stoptime)) {
            sb.append("&stoptime=" + stoptime);
        }
        HttpGet httpGet = new HttpGet(sb.toString());
        // 设置参数
        HttpParams params = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(params, 5000);

        HttpConnectionParams.setSoTimeout(params, 5000);
        // 执行请求的对象
        HttpClient client = new DefaultHttpClient(params);
        try {
            // 执行请求的对象
            HttpResponse resp = client.execute(httpGet);
            if (resp.getStatusLine().getStatusCode() == 200) {
                // 获取响应 的数据
                HttpEntity entity = resp.getEntity();
                data = EntityUtils.toString(entity, "utf-8");
                JSonObject code = JSONObject.parseObject(data);
                result = code.getString("status");
            }

        } catch (Exception e) {
            System.out.println("认证操失败" + e.getMessage());
        }
        return result;
    }


    public static String doPostForm(String url, Map map) throws Exception {
        String result = "";
        String resultString = "";
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(550000).setConnectTimeout(550000)
                .setConnectionRequestTimeout(550000).setStaleConnectionCheckEnabled(true).build();
        client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
        // client = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(url);

        HttpPost httpPost = new HttpPost(uriBuilder.build());
        httpPost.setHeader("Connection", "Keep-Alive");
        httpPost.setHeader("Charset", "utf-8");
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        Iterator> it = map.entrySet().iterator();
        List params = new ArrayList();

        while (it.hasNext()) {
            Map.Entry entry = it.next();
            NamevaluePair pair = new BasicNamevaluePair(entry.getKey(), entry.getValue());
            params.add(pair);
        }

        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        try {
            response = client.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "utf-8");
                    System.out.println("单点登录返回结果=========="+result);
                    JSonObject content = JSONObject.parseObject(result);
                    resultString = content.getString("content");
                }
            }
        } catch (ClientProtocolException e) {
            throw new RuntimeException("创建连接失败" + e);
        } catch (IOException e) {
            throw new RuntimeException("创建连接失败" + e);
        }
        return resultString;
    }


    public static String doPostJsons(String url, String json) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        String result = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            JSonObject content = JSONObject.parseObject(resultString);
            result = content.getString("content");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result;
    }
    


public class SSLClient extends DefaultHttpClient {
    public SSLClient() throws Exception{
        super();
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/705146.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号