您的位置:首页 > 理论基础 > 计算机网络

HttpURLCollection调用短信接口

2014-12-11 16:36 274 查看
public String sendData() {

//更新发送时间
updateSendTime();
URL url;
try {
//System.out.println(postUrl+"?"+postData.toString());
url = new URL(postUrl+"?"+postData.toString());
hconn = (HttpURLConnection) url.openConnection();
// 设置提交方式
hconn.setRequestMethod("POST");
// 设定传送的内容类型是可序列化的java对象
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
hconn.setRequestProperty("Content-type",
"application/x-java-serialized-object");
hconn.setRequestProperty("Connection", "Keep-Alive");
// http正文内,因此需要设为true, 默认情况下是false;
hconn.setDoOutput(true);
// Post 请求不能使用缓存
hconn.setUseCaches(false);

hconn.setRequestProperty("Content-Length", "" + postData.length());
//OutputStreamWriter out = new OutputStreamWriter(
//
hconn.getOutputStream(), "UTF-8");
//out.write(postData.toString());
//out.flush();
//out.close();

// 获取响应状态
if (hconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
// System.out.println("OK");
} else {
System.out.println(hconn.getResponseCode());
}

// 获取响应内容体
String line, result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(
hconn.getInputStream(), "UTF-8"));
while (null != (line = in.readLine())) {
result += line + "\n";
}
in.close();
return result;
// System.out.print(result);

} catch (Exception e) {
// TODO Auto-generated catch block
errlog(e);
e.printStackTrace();
}

return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  短信