http通过get请求请求页面数据

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class httpclient {
static CloseableHttpClient closeableHttpClient = null;
static CloseableHttpClient httpsClient = null;
static final String CHARSET = “UTF-8”;

static {
    RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(500).build();
    httpclient.closeableHttpClient = HttpClientBuilder.create().disableContentCompression().setDefaultRequestConfig(config).build();
    createSSLHttpClient();

}

public static void createSSLHttpClient() {
    try {
        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(500).build();
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                return true;
            }
        }).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        httpsClient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(config).build();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    }
}


public static String doPostByJson(String apiUrl, Object json) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String httpStr = null;
    HttpPost httpPost = new HttpPost(apiUrl);


    CloseableHttpResponse response = null;
    try {
        StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");// 解决中文乱码问题
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");

        httpPost.setEntity(stringEntity);
        httpPost.addHeader("username", "acrel");
        httpPost.addHeader("password", "acrel001");
        response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        httpStr = EntityUtils.toString(entity, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (response != null) {
            try {
                EntityUtils.consume(response.getEntity());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return httpStr;
}


public static List<String> load(final String vuserno) {
    try {
        String url = "http://192.168.xxx.xxx:8007/cuDeviceType/getDevTypeAndDeviceTree";
        List<NameValuePair> pairs = new ArrayList<NameValuePair>() {{
            add(new BasicNameValuePair("customerCode", vuserno));
            add(new BasicNameValuePair("flag", "3"));
        }};
        url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, "utf-8"));
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            httpGet.abort();
            throw new RuntimeException("请求失败,错误码:" + statusCode);
        }
        HttpEntity entity = response.getEntity();
        String result = null;
        if (entity != null) {
            result = EntityUtils.toString(entity, CHARSET);
        }
        System.out.println(result);

        EntityUtils.consume(entity);
        response.close();
        JSONObject jsonObject = JSON.parseObject(result);
        String rest2 = jsonObject.getString("data");
        if (rest2.length() == 0) {
            return null;
        }
        List<String> concodelist = Arrays.asList(rest2.split(","));
        return concodelist;

    } catch (Exception e) {
        log.info("http链接" + e.getMessage());
    }
    return null;
}


public static List<String> loads(String vuserno,String Cudate) {
    try {
        String url = "https://www.xxxxxx.cn/IECP/dataToMonthCompany";
        List<NameValuePair> pairs = new ArrayList<NameValuePair>() {{
            add(new BasicNameValuePair("customer", vuserno));
            add(new BasicNameValuePair("month", Cudate));
        }};
        url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, "utf-8"));
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpsClient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            httpGet.abort();
            throw new RuntimeException("请求失败,错误码:" + statusCode);
        }
        HttpEntity entity = response.getEntity();
        String result = null;
        if (entity != null) {
            result = EntityUtils.toString(entity, CHARSET);
        }
        System.out.println(result);

        EntityUtils.consume(entity);
        response.close();
        JSONObject jsonObject = JSON.parseObject(result);
        String rest2 = jsonObject.getString("data");

        if (rest2.length() == 0) {
            return null;
        }
        else {
            String newString = rest2.replace("[\"","");
            rest2= newString.replaceAll("\"]","");

        }
        List<String> concodelist = Arrays.asList(rest2.split(","));
        return concodelist;
    } catch (Exception e) {
        log.info("http链接" + e.getMessage());
    }
    return null;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值