tomcat_zhu
zhuzhihongNO1
采纳率40%
2018-03-01 08:30

调用https协议接口获取不到数据,代码如下

20
已采纳
 /**
     * 关键字查询商品,无分享赚
     * @param searchKey
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value="goodsListPlus",method = RequestMethod.POST)
    public String goodsListPlus(String searchKey,String pageIndex,String pageSize){
        String searchUrl="https://xxx/qwd_searchitem_ex?g_tk=200330352&pageindex="+pageIndex+"&pagesize="+pageSize+"&key="+searchKey+"&uniquespu=1&storestatus=1&ie=utf-8&text="+searchKey+"&_=1513671972404";
        String resultStr=httpsRequest(searchUrl, "GET", null,null);
        System.out.println("---处理前结果---"+resultStr);

        if(null!=resultStr &&!"".equals(resultStr)){
            ProductResult result = JSON.parseObject(resultStr, ProductResult.class);

            List<ProductInfo> productInfos = result.getSku();
            List<ProductInfo> tempInfos = new ArrayList<ProductInfo>();
            ProductResult tempResult = new ProductResult();
            if (null!=productInfos && !productInfos.isEmpty()) {
                for (int i = 0; i < productInfos.size(); i++) {
                    ProductInfo productInfo = new ProductInfo();
                    productInfo = productInfos.get(i);
                    tempInfos.add(productInfo);
                }
                tempResult.setSku(tempInfos);
                tempResult.setErrCode(result.getErrCode());
                tempResult.setMsg(result.getMsg());
                tempResult.setRetCode(result.getRetCode());
                tempResult.setSize(result.getSize());

                resultStr = JSON.toJSONString(tempResult);

                System.out.println("处理后结果---"+resultStr);
            }
        }


        return resultStr;
    }
    /*
     * 处理https GET/POST请求
     * 请求地址、请求方法、参数
     * */
    public static String httpsRequest(String requestUrl,String requestMethod,String outputStr,String cookie){
        StringBuffer buffer=null;
        try{
        //创建SSLContext
        SSLContext sslContext=SSLContext.getInstance("SSL");
        TrustManager[] tm={new MyX509TrustManager()};
        //初始化
        sslContext.init(null, tm, new java.security.SecureRandom());
        //获取SSLSocketFactory对象
        SSLSocketFactory ssf=sslContext.getSocketFactory();
        URL url=new URL(requestUrl);
        HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod(requestMethod);
        if (null!=cookie) {
            conn.setRequestProperty("Cookie", cookie);
        }
        //设置当前实例使用的SSLSoctetFactory
        conn.setSSLSocketFactory(ssf);
        conn.connect();
        //往服务器端写内容
        if(null!=outputStr){
            OutputStream os=conn.getOutputStream();
            os.write(outputStr.getBytes("utf-8"));
            os.close();
        }

        //读取服务器端返回的内容
        InputStream is=conn.getInputStream();
        InputStreamReader isr=new InputStreamReader(is,"utf-8");
        BufferedReader br=new BufferedReader(isr);
        buffer=new StringBuffer();
        String line=null;
        while((line=br.readLine())!=null){
            buffer.append(line);
        }
        is.close();
        isr.close();
        br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return buffer.toString();
    }


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • qq_36977403 qq_36977403 3年前

    ublic static CloseableHttpClient createSSLClientDefault(){
    try {
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
    //信任所有
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    return true;
    }
    }).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
    return HttpClients.custom().setSSLSocketFactory(sslsf).build();
    } catch (KeyManagementException e) {
    e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (KeyStoreException e) {
    e.printStackTrace();
    }
    return HttpClients.createDefault();
    }

    点赞 评论 复制链接分享
  • u012216293 影迹 3年前

    在socket里面获取数据

    点赞 评论 复制链接分享

相关推荐