java后台接口调用

来源:互联网 发布:足彩数据接口 编辑:程序博客网 时间:2024/05/01 09:08
package *.*.*.*;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class SmsSender {


public static boolean sendmessage(String code, String content, String mobile) {
String requestUrl = "http://**?cpid=??&cppwd=??&da={code}{Mobile}&dc=15&sm={content}";
requestUrl = requestUrl.replace("{code}", code);
requestUrl = requestUrl.replace("{content}", encodeHexStr(15, content));
requestUrl = requestUrl.replace("{Mobile}", mobile);
// 发送并解析结果
try {
// 发送并获取返回结果
String json = httpRequest(requestUrl);
if (json.indexOf("mterrcode") != -1) {
String str = json.substring(json.indexOf("mterrcode") + 10, json.length()).toString();
String mterrcode = "000";
if (mterrcode.equals(str)) {
return true;
} else {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}


/**
* 发送并获取返回结果

* @param requestUrl
* @return
*/
public static String httpRequest(String requestUrl) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();


httpUrlConn.setDoOutput(false);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);


httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();


// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();


} catch (Exception e) {
}
return buffer.toString();
}


/**
* 将普通字符串转换成Hex编码字符串

* @param dataCoding
*            编码格式,15表示GBK编码,8表示UnicodeBigUnmarked编码,0表示ISO8859-1编码
* @param realStr
*            普通字符串
* @return Hex编码字符串
*/
public static String encodeHexStr(int dataCoding, String realStr) {
String hexStr = null;


if (realStr != null) {
byte[] data = null;
try {
data = realStr.getBytes(getEncoding(dataCoding));


if (data != null) {
hexStr = byteArr2HexStr(data);
}
} catch (Exception e) {
}
}
return hexStr;
}


/**
* 根据编码值获取字符集名称

* @param dataCoding
* @return
*/
public static String getEncoding(int dataCoding) {
switch (dataCoding) {
case 15:
return "GBK";
default:
return null;
}
}


/**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程

* @param arrB
*            需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
*             本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}


}
0 0