package com.sxl.util;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/* 请关注微信公众号:bysjbng,各种免费毕设相关模板提供下载*/
public class Client {
/*
* webservice服务器定义
*/
//调用注册方法可能不成功。
//java.io.IOException: Server returned HTTP response code: 400 for URL: http://sdk2.zucp.net:8060/webservice.asmx。
//如果出现上述400错误,请参考第102行。
//如果您的系统是utf-8,收到的短信可能是乱码,请参考第102,295行
//可以根据您的需要自行解析下面的地址
//http://sdk2.zucp.net:8060/webservice.asmx?wsdl
private String serviceURL = "http://sdk2.zucp.net:8060/webservice.asmx";
private String sn = "";// 序列号
private String password = "";
private String pwd = "";// 密码
/*
* 构造函数
*/
public Client(String sn, String password)
throws UnsupportedEncodingException {
this.sn = sn;
this.password = password;
this.pwd = this.getMD5(sn + password);
}
/*
* 方法名称:getMD5
* 功 能:字符串MD5加密
* 参 数:待转换字符串
* 返 回 值:加密之后字符串
*/
public String getMD5(String sourceStr) throws UnsupportedEncodingException {
String resultStr = "";
try {
byte[] temp = sourceStr.getBytes();
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(temp);
// resultStr = new String(md5.digest());
byte[] b = md5.digest();
for (int i = 0; i < b.length; i++) {
char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = digit[(b[i] >>> 4) & 0X0F];
ob[1] = digit[b[i] & 0X0F];
resultStr += new String(ob);
}
return resultStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
/*
* 方法名称:register
* 功 能:注册
* 参 数:对应参数 省份,城市,行业,企业名称,联系人,电话,手机,电子邮箱,传真,地址,邮编
* 返 回 值:注册结果(String)
*/
public String register(String province, String city, String trade,
String entname, String linkman, String phone, String mobile,
String email, String fax, String address, String postcode) {
String result = "";
String soapAction = "http://tempuri.org/Register";
String xml = "";
xml += "
";
xml += "";
xml += "";
xml += "" + sn + "";
xml += "" + password + "";
xml += "" + province + "";
xml += "" + city + "";
xml += "" + trade + "";
xml += "" + entname + "";
xml += "" + linkman + "";
xml += "" + phone + "";
xml += "" + mobile + "";
xml += "" + email + "";
xml += "" + fax + "";
xml += "" + address + "";
xml += "" + postcode + "";
xml += "";
xml += "";
xml += "";
xml += "";
URL url;
try {
url = new URL(serviceURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
bout.write(xml.getBytes());
//bout.write(xml.getBytes("GBK"));
byte[] b = bout.toByteArray();
httpconn.setRequestProperty("Content-Length", String
.valueOf(b.length));
httpconn.setRequestProperty("Content-Type",
"text/xml; charset=gb2312");
httpconn.setRequestProperty("SOAPAction", soapAction);
httpconn.setRequestMethod("POST");
httpconn.setDoInput(true);
httpconn.setDoOutput(true);
OutputStream out = httpconn.getOutputStream();
out.write(b);
out.close();
InputStreamReader isr = new InputStreamReader(httpconn
.getInputStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while (null != (inputLine = in.readLine())) {
Pattern pattern = Pattern
.compile("
(.*)");
Matcher matcher = pattern.matcher(inputLine);
while (matcher.find()) {
result = matcher.group(1);
}
}
in.close();
return new String(result.getBytes(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/*
* 方法名称:chargeFee
* 功 能:充值
* 参 数:充值卡号,充值密码
* 返 回 值:操作结果(String)
*/
public String chargeFee(String cardno, String cardpwd) {
String result = "";
String soapAction = "http://tempuri.org/ChargUp";
String xml = "";
xml += "
";
xml += "";
xml += "";
xml += "" + sn + "";
xml += "" + password + "";
xml += "" + cardno + "";
xml += "" + cardpwd + "";
xml += "";
xml += "";
xml += "";
URL url;
try {
url = new URL(serviceURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
bout.write(xml.getBytes());
byte[] b = bout.toByteArray();
httpconn.setRequestProperty("Content-Length", String
.valueOf(b.length));
httpconn.setRequestProperty("Content-Type",
"text/xml; charset=gb2312");
httpconn.setRequestProperty("SOAPAction", soapAction);
httpconn.setRequestMethod("POST");
httpconn.setDoInput(true);
httpconn.setDoOutput(true);
OutputStream out = httpconn.getOutputStream();
out.write(b);
out.close();
InputStreamReader isr = new InputStreamReader(httpconn
.getInputStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while (null != (inputLine = in.readLine())) {
Pattern pattern = Pattern
.compile("
(.*)");
Matcher matcher = pattern.matcher(inputLine);
while (matcher.find()) {
result = matcher.group(1);
}
}
in.close();
// return result;
return new String(result.getBytes(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/*
* 方法名称:getBalance
* 功 能:获取余额
* 参 数:无
* 返 回 值:余额(String)
*/
public String getBalance() {
String result = "";
String soapAction = "http://tempuri.org/balance";
String xml = "";
xml += "
";
xml += "";
xml += "";
xml += "" + sn + "";
xml += "" + pwd + "";
xml += "";
xml += "";
xml += "";
URL url;
try {
url = new URL(serviceURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) connection;
ByteArrayOutputStream bout = new ByteArrayOut