微信小程序java8,调用接口webapi

By admin in 4858.com on 2019年8月11日

java异常:java.lang.NullPointerException,

 1 /**
 2 * 功能:空指针异常测试
 3 */
 4 /*    Object[] parameters1=null;
 5 if(parameters1.length>0&&parameters1!=null) {  //抛异常:java.lang.NullPointerException
 6 System.out.println("1");
 7 }
 8 System.out.println("2");
 9 
10 Object[] parameters2=null;
11 if(parameters2!=null&&parameters2.length>0) { //4
12 System.out.println("3");
13 }
14 System.out.println("4");*/

 

1
/** 2 * 成效:空指针分外测量检验 3 */ 4 /* Object[] parameters1=null;
5 if(parameters1.length0parameters1!=null) { //抛异常…

java使用httpclient发送post央求示例

原理:实现get post 请求

行使java8 java7  java6 解密微信小程序encryptedData能够回蒙受一些荒唐

JAVA工程师介绍,代码如下:

参考:http://www.cnblogs.com/vitre/p/5474166.html

1.java.security.NoSuchAlgorithmException: Cannot find any provider
supporting AES/CBC/PKCS7Padding

package org.ssi.util;

package com.dataMonitor.bo.common;
import java.io.BufferedReader;
import java.io.IOException;
4858.com,import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

2.Java.security.NoSuchProviderException: no such provider: BC 

import java.io.IOException;

import net.sf.json.JSONObject;
import java.io.*;
微信小程序java8,调用接口webapi。import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.model.Model;

 

import java.util.ArrayList;

/**

缓和方式:

import java.util.List;

  • 用于模拟HTTP乞求中GET/POST格局
  • @author landa

1.下载bcprov-jdk15on:jar,下再地址:

import net.sf.json.JSONArray;

/
public class Rbsc {
/
*
* 发送GET请求
*
* @param url
* 目标地方
* @param parameters
* 乞请参数,Map类型。
* @return 远程响应结果
/
public static String sendGet(String url, Map<String, String>
parameters) {
String result=””;
BufferedReader in = null;// 读取响应输入流
StringBuffer sb = new StringBuffer();// 存款和储蓄参数
String params = “”;// 编码之后的参数
try {
// 编码央浼参数
if(parameters.size()==1){
for(String name:parameters.keySet()){
sb.append(name).append(“=”).append(
java.net.URLEncoder.encode(parameters.get(name),
“UTF-8”));
}
params=sb.toString();
}else{
for (String name : parameters.keySet()) {
sb.append(name).append(“=”).append(
java.net.URLEncoder.encode(parameters.get(name),
“UTF-8”)).append(“&”);
}
String temp_params = sb.toString();
params = temp_params.substring(0, temp_params.length() – 1);
}
String full_url = url + “?” + params;
System.out.println(full_url);
// 创建URL对象
java.net.URL connURL = new java.net.URL(full_url);
// 打开URL连接
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection)
connURL
.openConnection();
// 设置通用属性
httpConn.setRequestProperty(“Accept”, “
/*”);
httpConn.setRequestProperty(“Connection”, “Keep-Alive”);
httpConn.setRequestProperty(“User-Agent”,
“Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)”);
// 创立实际的连天
httpConn.connect();
// 响应尾部获取
Map<String, List<String>> headers =
httpConn.getHeaderFields();
// 遍历全部的响应头字段
for (String key : headers.keySet()) {
System.out.println(key + “\t:\t” + headers.get(key));
}
// 定义BufferedReader输入流来读取UPRADOL的响应,并安装编码情势
in = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), “UTF-8”));
String line;
// 读取重返的原委
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result ;
}

2.解密代码

import org.apache.commons.lang.exception.ExceptionUtils;

/** 
 * 发送POST请求 
 *  
 * @param url 
 *            目的地址 
 * @param parameters 
 *            请求参数,Map类型。 
 * @return 远程响应结果 
 */  
public static String httpPostWithJSON(String url,String par)  {

    HttpPost httpPost = new HttpPost(url);
    CloseableHttpClient client = HttpClients.createDefault();
    String respContent = null;

    StringEntity entity = new StringEntity(par,"utf-8");//解决中文乱码问题  
    //System.out.println(entity.toString());
    entity.setContentEncoding("UTF-8");  
    entity.setContentType("application/json");  
    httpPost.setEntity(entity);

     try{
    HttpResponse resp = client.execute(httpPost);
    System.out.println(resp.getStatusLine().getStatusCode());
    if(resp.getStatusLine().getStatusCode() == 200) {
      HttpEntity he = resp.getEntity();
      respContent = EntityUtils.toString(he,"UTF-8");

    }

     } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     return respContent;

  }



public static void main(String[] args) throws Exception {

    JSONObject json = new JSONObject(); 
    json.put("ZSR",0);
    json.put("YLSR",0);
    json.put("MZSR",0);
    json.put("GHSR",0);
    json.put("ZCSR",0);
    json.put("JCSR",0);
    json.put("HYSR",0);
    json.put("ZLSR",0);
    json.put("SSSR",0);
    json.put("WSCLSR",0);
    json.put("YPSR",0);
    json.put("ZYYPSR",0);
    json.put("YSFWFSR",0);
    json.put("QTMZSR",0);
    json.put("ZYSR",0);
    json.put("CWSR",0);
    json.put("ZCSR_ZY",0);
    json.put("JCSR_ZY",0);
    json.put("HYSR_ZY",0);
    json.put("ZLSR_ZY",0);
    json.put("SSSR_ZY",0);
    json.put("HLSR_ZY",0);
    json.put("WSCLSR_ZY",0);
    json.put("YPSR_ZY",0);
    json.put("ZYYPSR_ZY",0);
    json.put("YSFWFSR_ZY",0);
    json.put("QTZYSR",0);
    json.put("JBYWSR",0);
    json.put("QZZYSR",0);
    json.put("ZQTSR",0);
    json.put("YBHZYLSR",0);
    json.put("YBMLWSR",0);
    json.put("YBMLNSR",0);
    json.put("MLNBXDYLFY",0);
    json.put("ZFY",0);
    json.put("YLYWCB",0);
    json.put("WSCLF",0);
    json.put("YPZC",0);
    json.put("ZYZC",0);
    json.put("GLFY",0);
    json.put("GGWSZC",0);
    json.put("ZLZRC",0);
    json.put("MZHJZRCS",0);
    json.put("YYZLRCS",0);
    json.put("PTMZRCS",0);
    json.put("CYRS",0);
    json.put("SWRS",0);
    json.put("RYRCS",0);
    json.put("RYRTS",0);
    json.put("ZYBRSSZH",0);
    json.put("YJSSCS",0);
    json.put("EJSSCS",0);
    json.put("SJSSCS",0);
    json.put("SIJSSCS",0);
    json.put("YWRQ","2017-01-21");


    String re= httpPostWithJSON("http://p.zjgws.com/QA/WN/RiBao/Add?YLDWCODE=467223515",json.toString());
    System.out.println(re);
  }

static{
Security.addProvider(new BouncyCastleProvider());
}

import org.apache.commons.logging.Log;

}

// 被加密的数目
byte[] dataByte = Base64.decodeBase64(encryptedData);
// 加密秘钥
byte[] keyByte = Base64.decodeBase64(sessionkey);
// 偏移量
byte[] ivByte = Base64.decodeBase64(iv);
try {
// Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS7Padding”,”BC”);

import org.apache.commons.logging.LogFactory;

Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS7Padding”,”BC”);
SecretKeySpec spec = new SecretKeySpec(keyByte, “AES”);
AlgorithmParameters parameters =
AlgorithmParameters.getInstance(“AES”);
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, “UTF-8”);

import org.apache.http.HttpResponse;

}

import org.apache.http.HttpStatus;

 

import org.apache.http.NameValuePair;

result 正是解密的数码

import org.apache.http.client.HttpClient;

 

import org.apache.http.client.entity.UrlEncodedFormEntity;

 

import org.apache.http.client.methods.HttpPost;

若果对您有协理,请不吝打赏

import org.apache.http.impl.client.DefaultHttpClient;

4858.com 1

import org.apache.http.message.BasicNameValuePair;

 

import org.apache.http.protocol.HTTP;

 

import org.apache.http.util.EntityUtils;

 

public class APIHttpClient {

//接口地址

private String apiURL = “”;

private Log logger = LogFactory.getLog(this.getClass());

private static final String pattern = “yyyy-MM-dd HH:mm:ss:SSS”;

private HttpClient httpClient = null;

private HttpPost method = null;

private long startTime = 0L;

private long endTime = 0L;

private int status = 0;

/**

* 接口地址

* @param url

*/

public APIHttpClient(String url){

if(url != null)

{

this.apiURL = url;

}

if(apiURL != null)

{

httpClient = new DefaultHttpClient();

method = new HttpPost(apiURL);

}

}

/**

* 调用 API

* @param parameters

* @return

*/

public String post(String parameters)

{

String body = null;

logger.info(“parameters:” + parameters);

if(method != null parameters != null !””.equals(parameters.trim()))

{

JSONArray jsonObject = JSONArray.fromObject(parameters);

logger.info(“json:” + jsonObject.toString());

try{

Listlt;NameValuePairgt; params=new ArrayListlt;NameValuePairgt;();

//创建三个NameValuePair数组,用于存款和储蓄欲传送的参数

params.add(new BasicNameValuePair(“data”,parameters));

//增加参数

method.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));

startTime = System.currentTimeMillis();

//设置编码

HttpResponse response=httpClient.execute(method);

endTime = System.currentTimeMillis();

int statusCode = response.getStatusLine().getStatusCode();

logger.info(“statusCode:” + statusCode);

logger.info(“调用API 开支时间(单位:阿秒):” + (end提姆e – start提姆e));

if(statusCode != HttpStatus.SC_OK){

logger.error(“Method failed:”+response.getStatusLine());

status = 1;

}

//Read the response body

body=EntityUtils.toString(response.getEntity());

}catch(IOException e){

//产生网络非常

logger.error(“exception

occurred!\n”+ExceptionUtils.getFullStackTrace(e));

//网络错误

status = 3;

}

finally{

logger.info(“调用接口状态:” + status);

}

}

return body;

}

/**

* 0.成功 1.实行办法失败 2.商业事务错误 3.互联网错误

* @return the status

*/

public int getStatus() {

return status;

}

/**

* @param status the status to set

*/

public void setStatus(int status) {

this.status = status;

}

/**

* @return the startTime

*/

public long getStartTime() {

return startTime;

}

/**

* @return the endTime

*/

public long getEndTime() {

return endTime;

}

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有