java微信开发接口代码,微信编程接口
java如何创建微信自定义接口
如下:
创新互联成都网站建设定制网站设计,是成都营销推广公司,为成都白乌鱼提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站维护热线:028-86922220
public static String do_post(String url, ListNameValuePair name_value_pair) throws IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
public static String do_get(String url) throws ClientProtocolException, IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
如何使用java开发微信公众平台接口
1、首先,要在微信公众平台给你的账号申请到“高级功能” ;前台也就是菜单要想个性化设置必须要有这个功能,不然你只能添加菜单和关闭,但不能删除,还有自动回复也是。
2、后台要看你自己了。
微信开发平台中有个接口是上传多媒体文件,我用的是java 开发的,我怎么样才能在后台实现呢?代码如下:
/**
* 文件上传到微信服务器
* @param fileType 文件类型
* @param filePath 文件路径
* @return JSONObject
* @throws Exception
*/
public static JSONObject send(String fileType, String filePath) throws Exception {
String result = null;
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
throw new IOException("文件不存在");
}
/**
* 第一部分
*/
URL urlObj = new URL(""+ getAccess_token() + "type="+fileType+"");
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false); // post方式不能使用缓存
// 设置请求头信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// 设置边界
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary="+ BOUNDARY);
// 请求正文信息
// 第一部分:
StringBuilder sb = new StringBuilder();
sb.append("--"); // 必须多两道线
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"file\";filename=\""+ file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
// 获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
// 输出表头
out.write(head);
// 文件正文部分
// 把文件已流文件的方式 推入到url中
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
// 结尾部分
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
//System.out.println(line);
buffer.append(line);
}
if(result==null){
result = buffer.toString();
}
} catch (IOException e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
throw new IOException("数据读取异常");
} finally {
if(reader!=null){
reader.close();
}
}
JSONObject jsonObj =new JSONObject(result);
return jsonObj;
}
怎么用java调用微信支付接口
java调用微信支付接口方法:
RequestHandler requestHandler = new RequestHandler(super.getRequest(),super.getResponse());
//获取token //两小时内有效,两小时后重新获取
Token = requestHandler.GetToken();
//更新token 到应用中
requestHandler.getTokenReal();
System.out.println("微信支付获取token=======================:" +Token);
//requestHandler 初始化
requestHandler.init();
requestHandler.init(appid,appsecret, appkey,partnerkey, key);
// --------------------------------本地系统生成订单-------------------------------------
// 设置package订单参数
SortedMapString, String packageParams = new TreeMapString, String();
packageParams.put("bank_type", "WX"); // 支付类型
packageParams.put("body", "xxxx"); // 商品描述
packageParams.put("fee_type", "1"); // 银行币种
packageParams.put("input_charset", "UTF-8"); // 字符集
packageParams.put("notify_url", ""); // 通知地址 这里的通知地址使用外网地址测试,注意80端口是否打开。
packageParams.put("out_trade_no", no); // 商户订单号
packageParams.put("partner", partenerid); // 设置商户号
packageParams.put("spbill_create_ip", super.getRequest().getRemoteHost()); // 订单生成的机器IP,指用户浏览器端IP
packageParams.put("total_fee", String.valueOf(rstotal)); // 商品总金额,以分为单位
// 设置支付参数
SortedMapString, String signParams = new TreeMapString, String();
signParams.put("appid", appid);
signParams.put("noncestr", noncestr);
signParams.put("traceid", PropertiesUtils.getOrderNO());
signParams.put("timestamp", timestamp);
signParams.put("package", packageValue);
signParams.put("appkey", this.appkey);
// 生成支付签名,要采用URLENCODER的原始值进行SHA1算法!
String sign ="";
try {
sign = Sha1Util.createSHA1Sign(signParams);
} catch (Exception e) {
e.printStackTrace();
}
// 增加非参与签名的额外参数
signParams.put("sign_method", "sha1");
signParams.put("app_signature", sign);
// api支付拼包结束------------------------------------
//获取prepayid
String prepayid = requestHandler.sendPrepay(signParams);
System.out.println("prepayid :" + prepayid);
// --------------------------------生成完成---------------------------------------------
//生成预付快订单完成,返回给android,ios 掉起微信所需要的参数。
SortedMapString, String payParams = new TreeMapString, String();
payParams.put("appid", appid);
payParams.put("noncestr", noncestr);
payParams.put("package", "Sign=WXPay");
payParams.put("partnerid", partenerid);
payParams.put("prepayid", prepayid);
payParams.put("appkey", this.appkey);
//这里除1000 是因为参数长度限制。
int time = (int) (System.currentTimeMillis() / 1000);
payParams.put("timestamp",String.valueOf(time));
System.out.println("timestamp:" + time);
//签名
String paysign ="";
try {
paysign = Sha1Util.createSHA1Sign(payParams);
} catch (Exception e) {
e.printStackTrace();
}
payParams.put("sign", paysign);
//拼json 数据返回给客户端
BasicDBObject backObject = new BasicDBObject();
backObject.put("appid", appid);
backObject.put("noncestr", payParams.get("noncestr"));
backObject.put("package", "Sign=WXPay");
backObject.put("partnerid", payParams.get("partnerid"));
backObject.put("prepayid", payParams.get("prepayid"));
backObject.put("appkey", this.appkey);
backObject.put("timestamp",payParams.get("timestamp"));
backObject.put("sign",payParams.get("sign"));
String backstr = dataObject.toString();
System.out.println("backstr:" + backstr);
return backstr;
====================到此为止,预付款订单已生成,并且已返回客户端====================
//坐等微信服务器通知,通知的地址就是生成预付款订单的notify_url
ResponseHandler resHandler = new ResponseHandler(request, response);
resHandler.setKey(partnerkey);
//创建请求对象
//RequestHandler queryReq = new RequestHandler(request, response);
//queryReq.init();
if (resHandler.isTenpaySign() == true) {
//商户订单号
String out_trade_no = resHandler.getParameter("out_trade_no");
System.out.println("out_trade_no:" + out_trade_no);
//财付通订单号
String transaction_id = resHandler.getParameter("transaction_id");
System.out.println("transaction_id:" + transaction_id);
//金额,以分为单位
String total_fee = resHandler.getParameter("total_fee");
//如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee
String discount = resHandler.getParameter("discount");
//支付结果
String trade_state = resHandler.getParameter("trade_state");
//判断签名及结果
if ("0".equals(trade_state)) {
//------------------------------
//即时到账处理业务开始
//------------------------------
System.out.println("----------------业务逻辑执行-----------------");
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
System.out.println("----------------业务逻辑执行完毕-----------------");
System.out.println("success"); // 请不要修改或删除
System.out.println("即时到账支付成功");
//给财付通系统发送成功信息,财付通系统收到此结果后不再进行后续通知
resHandler.sendToCFT("success");
//给微信服务器返回success 否则30分钟通知8次
return "success";
}else{
System.out.println("通知签名验证失败");
resHandler.sendToCFT("fail");
response.setCharacterEncoding("utf-8");
}
}else {
System.out.println("fail -Md5 failed");
如何使用微信sdk java版
1.首先我们新建一个Java开发包WeiXinSDK
2.包路径:com.ansitech.weixin.sdk
测试的前提条件:
假如我的公众账号微信号为:vzhanqun
我的服务器地址为:
下面我们需要新建一个URL的请求地址
我们新建一个Servlet来验证URL的真实性,具体接口参考
接入指南
3.新建com.ansitech.weixin.sdk.WeixinUrlFilter.java
这里我们主要是获取微信服务器法师的验证信息,具体验证代码如下
[java] view plain copy print?
package com.ansitech.weixin.sdk;
import com.ansitech.weixin.sdk.util.SHA1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeixinUrlFilter implements Filter {
//这个Token是给微信开发者接入时填的
//可以是任意英文字母或数字,长度为3-32字符
private static String Token = "vzhanqun1234567890";
@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("WeixinUrlFilter启动成功!");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//微信服务器将发送GET请求到填写的URL上,这里需要判定是否为GET请求
boolean isGet = request.getMethod().toLowerCase().equals("get");
System.out.println("获得微信请求:" + request.getMethod() + " 方式");
if (isGet) {
//验证URL真实性
String signature = request.getParameter("signature");// 微信加密签名
String timestamp = request.getParameter("timestamp");// 时间戳
String nonce = request.getParameter("nonce");// 随机数
String echostr = request.getParameter("echostr");//随机字符串
ListString params = new ArrayListString();
params.add(Token);
params.add(timestamp);
params.add(nonce);
//1. 将token、timestamp、nonce三个参数进行字典序排序
Collections.sort(params, new ComparatorString() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
//2. 将三个参数字符串拼接成一个字符串进行sha1加密
String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));
if (temp.equals(signature)) {
response.getWriter().write(echostr);
}
} else {
//处理接收消息
}
}
@Override
public void destroy() {
}
}
好了,不过这里有个SHA1算法,我这里也把SHA1算法的源码给贴出来吧!
4.新建com.ansitech.weixin.sdk.util.SHA1.java
[java] view plain copy print?
/*
* 微信公众平台(JAVA) SDK
*
* Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.
*
*
* 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
*
*
*
* 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.ansitech.weixin.sdk.util;
import java.security.MessageDigest;
/**
* pTitle: SHA1算法/p
*
* @author qsyangyangqisheng274@163.com
*/
public final class SHA1 {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Takes the raw bytes from the digest and formats them correct.
*
* @param bytes the raw bytes from the digest.
* @return the formatted bytes.
*/
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文转换成十六进制的字符串形式
for (int j = 0; j len; j++) {
buf.append(HEX_DIGITS[(bytes[j] 4) 0x0f]);
buf.append(HEX_DIGITS[bytes[j] 0x0f]);
}
return buf.toString();
}
public static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
messageDigest.update(str.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
5.把这个Servlet配置到web.xml中
[html] view plain copy print?
filter
description微信消息接入接口/description
filter-nameWeixinUrlFilter/filter-name
filter-classcom.ansitech.weixin.sdk.WeixinUrlFilter/filter-class
/filter
filter-mapping
filter-nameWeixinUrlFilter/filter-name
url-pattern/api/vzhanqun/url-pattern
/filter-mapping
好了,接入的开发代码已经完成。
6.下面就把地址URL和密钥Token填入到微信申请成为开发者模式中吧。
网站栏目:java微信开发接口代码,微信编程接口
分享地址:http://scyanting.com/article/hohdhh.html