后台调用http短信接口

两种方式,第一种只能用GET方式,POST方式会报 411错误:

sContent = URLEncoder.encode(request.getParameter("sContent"),"utf-8");
String contents="您申请的《药品出口证书》已经审批完毕,请登录河北省食药监局行政审批系统,在“通知单”菜单中彩色打印。";
path="http://192.168.5.42/BackManage/GwDuanxin.aspx?iMsgID=110&shouWenRen=11&shouWenTel="+shouWenTel+"&sContent="+contents;
try {
URL url=new URL(path);
HttpURLConnection conn =(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = in.readLine();
System.out.println(" </p> result: " + line);
int i_ret = conn.getResponseCode();
String sRet = conn.getResponseMessage();
System.out.println("sRet is: " + sRet);
System.out.println("i_ret is: " + i_ret);
return null;

} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
[quote][/quote] e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}


第二种直接用POST

PostMethod method = null;
try {
HttpClient client = new HttpClient();
//client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");//指定传送字符集为UTF-8格式
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);//设置连接超时时间为30秒(连接初始化时间)
method = new PostMethod(path);//带参数的那个请求的访问串
//path="http://192.168.5.42/BackManage/GwDuanxin.aspx?iMsgID=110&shouWenRen=11&shouWenTel="+shouWenTel+"&sContent="+contents;

method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk");//格式转换
// method.addParameter("iMsgID", "测试20110802");//短信内容
// method.addParameter("shouWenRen", "贾玉龙");//
// method.addParameter("shouWenTel", "18633020413");//
// method.addParameter("sContent", contents);//

int statusCode = client.executeMethod(method);
String info = new String(method.getResponseBody(),"gbk");
System.out.println(info);
// log.info("访问主机开始...");
if (statusCode != HttpStatus.SC_OK) {
//失败
System.out.println("发送失败");
}else{
//成功
System.out.println("发送成功");
}
//log.info("访问主机结束.");
client.getHttpConnectionManager().closeIdleConnections(1);
//log.info("访问主机:" + hostaddr[i] + "完成.");
}catch (Exception e) {
//log.error("无法访问主机:"+hostaddr[i]);
}finally{
if(method != null)
method.releaseConnection();
}
return null;
}



系统中用的GBK编码格式,用以上两种方式都会乱码, 还在处理乱码中....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值