JAVA访问联通http接口 提示登录信息失效 大家帮我看看
目前想集成一短信到OA系统中 在使用联通http接口过程中出了问题:调用接口登陆成功 发送失败 返回值-2 意思是登陆信息失效 我想知道是什么原因 大家帮我看看是否我的程序除了问题 以下是简单得测试程序:servlet:
Java代码
protected void doPost(HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse) throws ServletException,
IOException {
httpservletrequest.setCharacterEncoding("GBK");
httpservletresponse.setContentType("text/html;charset=GBK");
out = httpservletresponse.getWriter();
try {
String Ecode = "**********";
String user = "**********";
String pass = "**********";
SmsBaseBean smsbasebean = new SmsBaseBean();
String str = smsbasebean.ToLogin(Ecode, user, pass);
if (!str.equals("")) {
String[] userArray = str.split(",");
String corid = userArray[0];
String userid = userArray[1];
httpservletrequest.getSession().setAttribute("corid", corid);
httpservletrequest.getSession().setAttribute("userid", userid);
String phonNo = "******8,******";
String[] dstArray = phonNo.split(",");
// log.info("开始发送... ");
String mobile ="";
String msg = "";
String aa = (String) httpservletrequest.getSession().getAttribute(
"corid");
String bb = (String) httpservletrequest.getSession().getAttribute(
"userid");
if (CheckType(mobile)) {
// ToMobile();
} else {
String result0 = smsbasebean.ToUnicom(aa, bb,mobile, msg);
System.out.println("result0++++++++++++++++++++"+ result0);
}
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
--------------------编程问答-------------------- SmsBaseBean:
Java代码
package com.sms;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletRequest;
import javax.servlet.http.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* 发送短信 登陆bean
*
* @author Administrator
*
*/
public class SmsBaseBean implements Serializable {
/**
* 类的构造方法
*
*/
public SmsBaseBean() {
}
public String ToLogin(String Ecode, String user, String pass) {
String str=null;
try {
String corid = null;
String userid = null;
HashMap ReqParam = null;
String respPack = assembleResponsePack(Ecode, user, pass);
System.out.println("respPack++" + respPack);
String reqPack = Submitteres(respPack);
System.out.println("reqPack++" + reqPack);
ReqParam = ReqParam(reqPack);
corid = (String) ReqParam.get("corid");
userid = (String) ReqParam.get("userid");
str=corid+","+userid;
} catch (Exception ex) {
System.out.println("test..." + ex.toString());
}
return str;
}
/**
* 发送到移动 调用移动接口
*/
public void ToMobile() {
}
/**
* 发送到联通 调用联通接口
* @param
*/
public String ToUnicom(String corid,String userid,String mobile, String msg) {
String flag="";
try {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String InfoSendTime = formatter.format(currentTime);
InfoSendTime = InfoSendTime.substring(0, 4)
+ InfoSendTime.substring(5, 7)
+ InfoSendTime.substring(8, 10)
+ InfoSendTime.substring(11, 13)
+ InfoSendTime.substring(14, 16)
+ InfoSendTime.substring(17, 19);
String sendrespPack = SendResponsePack(corid, userid, mobile,
InfoSendTime);
//System.out.println("xml++" + sendrespPack);
flag = Submit(sendrespPack);
} catch (Exception ex) {
System.out.println("test..." + ex.toString());
}
return flag;
}
private HashMap ReqParam(String pack) throws Exception {
HashMap map = new HashMap();
DocumentBuilderFactory docBuilFac = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuil = null;
Document doc = null;
NodeList nl = null;
NodeList n2 = null;
String nodeValue = null;
docBuil = docBuilFac.newDocumentBuilder();
doc = docBuil.parse(new ByteArrayInputStream(pack.getBytes()));
// logusers : loguser , corid ,userid
nl = doc.getElementsByTagName("loguser");
Element element_sa = (Element) nl.item(0);
NodeList saDeviceType = element_sa.getElementsByTagName("corid");
nodeValue = saDeviceType.item(0).getFirstChild().getNodeValue();
map.put("corid", nodeValue);
NodeList saDeviceID = element_sa.getElementsByTagName("userid");
nodeValue = saDeviceID.item(0).getFirstChild().getNodeValue();
map.put("userid", nodeValue);
// corid
// nl = doc.getElementsByTagName("corid");
// nodeValue = nl.item(0).getFirstChild().getNodeValue();
// map.put("corid", nodeValue);
// userid
// nl = doc.getElementsByTagName("userid");
// nodeValue = nl.item(0).getFirstChild().getNodeValue();
// map.put("userid", nodeValue);
return map;
}
//写登陆信息XML
private String assembleResponsePack(String Ecode, String user, String pass) throws UnsupportedEncodingException {
StringBuffer pack = new StringBuffer();
pack.append("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
pack.append("<logusers>");
pack.append("<loguser>");
pack.append("<qiname>" + Ecode+ "</qiname>");
pack.append("<user>" + user + "</user>");
pack.append("<pass>" + pass + "</pass>");
pack.append("</loguser>");
pack.append("</logusers>");
return pack.toString();
}
//写信息内容那个XML
private String SendResponsePack(String CorId, String UserId, String mobile,
String sendtime) {
StringBuffer pack = new StringBuffer();
pack.append("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
pack.append("<msgs>");
pack.append("<msg>");
pack.append("<CorId>" + CorId + "</CorId>"); //企业ID
pack.append("<UserId>"+UserId+"</UserId>");//用户ID
pack.append("<InfoType>1</InfoType>");//固定填1
pack.append("<Stype>0</Stype>");//固定填0
pack.append("<MsgCode>16298</MsgCode>");//消息序列号(6位)当天的序列号不能重复,位数不够左边用0补齐
pack.append("<RecvUserMsisdn>13294235501</RecvUserMsisdn>");//接受信息的手机号码
pack.append("<Content>006700680064006b0073006700640065</Content>");//消息内容(unicode编码)
pack.append("<CodeType>8</CodeType>");//编码方式
pack.append("<InfoSendTime>"+sendtime+"</InfoSendTime>");//发送时间
pack.append("<codeflag>1065502******</codeflag>");//主叫号码
pack.append("</msg>");
return pack.toString();
}
接下: --------------------编程问答-------------------- 接上:
//提交登陆信息 返回登陆信息
private synchronized String Submitteres(String content) {
String xmlreturn = null;
try {
PostMethod post = new PostMethod(
"http://61.242.168.104/corsend/login.aspx");
post.setRequestBody(new String(content));
if (content.length() < 0x7fffffffL)
post.setRequestContentLength(content.length());
else
post.setRequestContentLength(-1L);
post.setRequestHeader("Content-type", "text/xml; charset=gb2312");
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
// xmlreturn = "Response status code: " + result + "Response body:
// "+ post.getResponseBodyAsString();
xmlreturn = post.getResponseBodyAsString();
post.releaseConnection();
} catch (Exception ex) {
System.out.println(ex.toString());
}
return xmlreturn;
}
// 提交发送信息 返回标志
private synchronized String Submit(String content) {
String xmlreturn = null;
try {
PostMethod post = new PostMethod(
"http://61.242.168.104/corsend/msgsend.aspx");
post.setRequestBody(new String(content));
if (content.length() < 0x7fffffffL)
post.setRequestContentLength(content.length());
else
post.setRequestContentLength(-1L);
post.setRequestHeader("Content-type", "text/xml; charset=gb2312");
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
// xmlreturn = "Response status code: " + result + "Response body:
// "+ post.getResponseBodyAsString();
xmlreturn = post.getResponseBodyAsString();
post.releaseConnection();
} catch (Exception ex) {
System.out.println(ex.toString());
}
return xmlreturn;
}
/**
* 把字符串转换成Unicode编码
*/
private String toUnicode(String strText, String code)
throws UnsupportedEncodingException {
char c;
String strRet = "";
int intAsc;
String strHex;
strText = new String(strText.getBytes("iso8859-1"), code);
for (int i = 0; i < strText.length(); i++) {
c = strText.charAt(i);
intAsc = (int) c;
if (intAsc > 128) {
strHex = Integer.toHexString(intAsc);
strRet = strRet + "" + strHex + ";";
} else {
strRet = strRet + c;
}
}
return strRet;
}
/**
* 把字符串以特定的字符分开 返回一个数组
*
*/
public String[] splitString(String _str, char _ch) {
int pre = 0;
ArrayList list = new ArrayList();
for (int i = 0; i < _str.length(); i++) {
if (_str.charAt(i) == _ch) {
if (i != pre)
list.add(_str.substring(pre, i));
pre = i + 1;
}
}
if (pre != _str.length()) {
list.add(_str.substring(pre, _str.length()));
}
return (String[]) list.toArray(new String[0]);
}
public String getTime(String s) {
SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
Date date = new Date(System.currentTimeMillis());
String s1 = simpledateformat.format(date);
return s1;
}
/**
* 字符替换方法
*/
public String replace(String s, String s1, String s2) {
for (int i = s.indexOf(s1); i > -1; i = s.indexOf(s1)) {
String s3 = s.substring(0, i);
String s4 = s.substring(i + s1.length(), s.length());
s = s3 + s2 + s4;
}
return s;
}
public Vector strSplit(String s, String s1) {
Vector vector = new Vector();
String s2 = s;
if (s.length() > 0) {
String s3;
for (; s.indexOf(s1) > -1; vector.addElement(s3)) {
s3 = s.substring(0, s.indexOf(s1));
s = s.substring(s.indexOf(s1) + 1);
}
if (s.indexOf(s1) == -1) {
String s4 = s.substring(0);
if (s4 != "") {
vector.addElement(s4);
}
}
}
return vector;
}
} --------------------编程问答-------------------- pack.append(" <MsgCode>16298 </MsgCode>");//消息序列号(6位)当天的序列号不能重复,位数不够左边用0补齐
--------------------编程问答-------------------- --------------------编程问答-------------------- 首先测试下移动端发过来的信息,服务器端是否正常接收和解析。
一般把数据进行转码为utf-8然后再传输。
服务器端有解码的操作。 --------------------编程问答--------------------
这个问题是我贴得实时候失误了 是6位得
wzt10 写到:
首先测试下移动端发过来的信息,服务器端是否正常接收和解析。
一般把数据进行转码为utf-8然后再传输。
服务器端有解码的操作。
这里用得似乎不是UTF-8Z编码
测试中提供得是类似这样得编码装换:
Function Chr2Unicode(byval str)
Dim st, t, i,t1
For i = 1 To Len(str)
t = Hex(AscW(Mid(str, i, 1)))
If (Len(t) < 4) Then
while (Len(t) < 4)
t = "0"&t
Wend
End If
t = Mid(t, 3) & Left(t, 2)
t1=mid(t,3,2)&mid(t,1,2)
st = st & t1
Next
Chr2Unicode = LCase(st)
End Function
而且这里出了个问题 java中字符转码成unicode 好像出问题了 google baidu上找到得那方法
转换得似乎不是服务器所规定得编码格式,函数余下:
private String toUnicode(String strText, String code)
throws UnsupportedEncodingException {
char c;
String strRet = "";
int intAsc;
String strHex;
strText = new String(strText.getBytes("iso8859-1"), code);
for (int i = 0; i < strText.length(); i++) {
c = strText.charAt(i);
intAsc = (int) c;
if (intAsc > 128) {
strHex = Integer.toHexString(intAsc);
strRet = strRet + "" + strHex + ";";
} else {
strRet = strRet + c;
}
}
return strRet;
}
有遇到此情况得吗? java中如何实现上面asp中函数得功能? --------------------编程问答--------------------
补充:移动开发 , 移动开发其他问题