java从第三方就接口获取数据的

更新时间:2019-03-08 10:09:00点击次数:343次
1 封装参数

   /**
     * 这里封装了对于分页记录的处理,totalPaht是返回的json数据中总条数的路径,如果没有翻页 该参数设置为null即可
     * 目前只支持json数据格式
     * @param para  参数集合
     * @param url   接口的地址
     * @param totalPath 总条数的json路径,默认返回的是json数据
     * @return
     * @throws IOException
     */
    public static  List<JSONObject> doGet(Map<String, String> para, String url, String totalPath) throws IOException {
        List<JSONObject> list = new ArrayList<>();
        try {
            int pageSize = StringUtils.isBlank(para.get("pageSize")) ? 10 : Integer.parseInt(para.get("pageSize"));
            int total = 0;
            int pageNum = 1;
            JSONObject rs = query(para, url);
            if (null == rs || rs.isEmpty())
                return list;
            int code = (int) JSONPath.eval(rs, "$.error_code");
            String reason = (String) JSONPath.eval(rs, "$.reason");
            Object result = JSONPath.eval(rs, "$.result");
            if (null == result) {
                return list;
            }
            if (!StringUtils.isEquals("ok", reason) || (code != 0)) {
                return list;
            }
            list.add(rs);
            /* 支持所有基本数据类型  total */
            if (!StringUtils.isBlank(totalPath)) {
                Object totalObject = JSONPath.eval(rs, totalPath) == null ? "" : JSONPath.eval(rs, totalPath);
                try{
                    total = ObjectUtils.isEmpty(totalObject) ? 0 : Integer.parseInt(totalObject.toString());
                }catch (Exception e){
                    total = 0;
                }
            }
            if (total == 0 || total <= pageSize)
                return list;
            pageNum++;
            for (; (pageNum * pageSize) < total; pageNum++) {
                para.put("pageNum",String.valueOf(pageNum));
                para.put("pageSize", String.valueOf(pageSize));
                rs = query(para, url);
                list.add(rs);
            }
            if (total < (pageNum * pageSize)) {
                para.put("pageNum",String.valueOf(pageNum));
                para.put("pageSize", String.valueOf(pageSize));
                rs = query(para, url);
                list.add(rs);
            }
        }catch (Exception e){
            System.out.println("GetDataFromEyeUtils doGet Error :" + e.getMessage());
        }
        para.clear();
        return list;
    }

    /**
     * 封装head参数,调用url连接方法
     * @param para
     * @param url
     * @return
     */
    public static  JSONObject query(Map<String, String> para, String url) {
        if (map == null) {
            throw new NullPointerException("请求不可以为空");
        }
        count++;
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization", HoldingConsts.TOKEN);
        String result = HttpKit.get(url, para, headers);
        System.out.println(result);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(result))
            jsonObject = JSON.parseObject(result);
        System.out.println(jsonObject.toJSONString());
        return jsonObject;
    }

1 建立连接
httpKitt 来自github的开源项目地址是
https://github.com/jfinal/jfinal/blob/master/src/main/java/com/jfinal/kit/HttpKit.java

/**
 * Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.kit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.servlet.http.HttpServletRequest;

/**
 * HttpKit
 */
public class HttpKit {

private HttpKit() {}

/**
* https 域名校验
*/
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}

/**
* https 证书管理
*/
private static class TrustAnyTrustManager implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;  
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
}

private static final String GET  = "GET";
private static final String POST = "POST";
private static String CHARSET = "UTF-8";

private static final SSLSocketFactory sslSocketFactory = initSSLSocketFactory();
private static final TrustAnyHostnameVerifier trustAnyHostnameVerifier = new HttpKit.TrustAnyHostnameVerifier();

private static SSLSocketFactory initSSLSocketFactory() {
try {
TrustManager[] tm = {new HttpKit.TrustAnyTrustManager() };
SSLContext sslContext = SSLContext.getInstance("TLS"); // ("TLS", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
return sslContext.getSocketFactory();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

public static void setCharSet(String charSet) {
if (StrKit.isBlank(charSet)) {
throw new IllegalArgumentException("charSet can not be blank.");
}
HttpKit.CHARSET = charSet;
}

private static HttpURLConnection getHttpConnection(String url, String method, Map<String, String> headers) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
URL _url = new URL(url);
HttpURLConnection conn = (HttpURLConnection)_url.openConnection();
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection)conn).setSSLSocketFactory(sslSocketFactory);
((HttpsURLConnection)conn).setHostnameVerifier(trustAnyHostnameVerifier);
}

conn.setRequestMethod(method);
conn.setDoOutput(true);
conn.setDoInput(true);

conn.setConnectTimeout(19000);
conn.setReadTimeout(19000);

conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");

if (headers != null && !headers.isEmpty()) {
for (Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}

return conn;
}

/**
* Send GET request
*/
public static String get(String url, Map<String, String> queryParas, Map<String, String> headers) {
HttpURLConnection conn = null;
try {
conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), GET, headers);
conn.connect();
return readResponseString(conn);
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (conn != null) {
conn.disconnect();
}
}
}

public static String get(String url, Map<String, String> queryParas) {
return get(url, queryParas, null);
}

public static String get(String url) {
return get(url, null, null);
}

/**
* Send POST request
*/
public static String post(String url, Map<String, String> queryParas, String data, Map<String, String> headers) {
HttpURLConnection conn = null;
try {
conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), POST, headers);
conn.connect();

if (data != null) {
OutputStream out = conn.getOutputStream();
out.write(data.getBytes(CHARSET));
out.flush();
out.close();
}

return readResponseString(conn);
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (conn != null) {
conn.disconnect();
}
}
}

public static String post(String url, Map<String, String> queryParas, String data) {
return post(url, queryParas, data, null);
}

public static String post(String url, String data, Map<String, String> headers) {
return post(url, null, data, headers);
}

public static String post(String url, String data) {
return post(url, null, data, null);
}

private static String readResponseString(HttpURLConnection conn) {
BufferedReader reader = null;
try {
StringBuilder ret;
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), CHARSET));
String line = reader.readLine();
if (line != null) {
ret = new StringBuilder();
ret.append(line);
} else {
return "";
}

while ((line = reader.readLine()) != null) {
ret.append('\n').append(line);
}
return ret.toString();
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
LogKit.error(e.getMessage(), e);
}
}
}
}

/**
* Build queryString of the url
*/
private static String buildUrlWithQueryString(String url, Map<String, String> queryParas) {
if (queryParas == null || queryParas.isEmpty()) {
return url;
}

StringBuilder sb = new StringBuilder(url);
boolean isFirst;
if (url.indexOf('?') == -1) {
isFirst = true;
sb.append('?');
}
else {
isFirst = false;
}

for (Entry<String, String> entry : queryParas.entrySet()) {
if (isFirst) {
isFirst = false;
} else {
sb.append('&');
}

String key = entry.getKey();
String value = entry.getValue();
if (StrKit.notBlank(value)) {
try {value = URLEncoder.encode(value, CHARSET);} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}
}
sb.append(key).append('=').append(value);
}
return sb.toString();
}

public static String readData(HttpServletRequest request) {
BufferedReader br = null;
try {
StringBuilder ret;
br = request.getReader();

String line = br.readLine();
if (line != null) {
ret = new StringBuilder();
ret.append(line);
} else {
return "";
}

while ((line = br.readLine()) != null) {
ret.append('\n').append(line);
}

return ret.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
/* 去掉 close() 否则后续 ActionReporter 中的 getPara() 在部分 tomcat 中会报 IOException : Stream closed
finally {
if (br != null) {
try {br.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
}
}*/
}

@Deprecated
public static String readIncommingRequestData(HttpServletRequest request) {
return readData(request);
}
}



本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息
  • 项目经理 点击这里给我发消息