import java.io.OutputStream;
import java.net.*;
import java.util.Date;
import java.text.*;
public class SendInfo2 {
public static void main(String[] args) {
boolean b = false;
String time = "";
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
time = df.format(new Date()); //使用特定格式显示日期和时间
b = sendBgtMessages("李斯提交了测试",time);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
System.out.print("212---"+time+"--"+b);
}
}
//短信发送功能
public static boolean sendBgtMessages(String msg,String send_time) throws Exception{
boolean b = false;
String address = "http://***/send.jsp";
StringBuffer sb = new StringBuffer();
sb.append("mobile=13788889999");
sb.append("&msgcontent="+msg);
sb.append("&sendtime="+send_time);
b = openConnection(address,sb);
return b;
}
//访问http链接
public static boolean openConnection(String address,StringBuffer params) throws Exception {
int HttpResult;
URL url = new URL(address);
//返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
urlconn.setDoOutput(true);
urlconn.setRequestMethod("POST");
//打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
urlconn.connect();
//post信息 ,这步很重要,不然就乱码
OutputStream os = urlconn.getOutputStream();
os.write(params.toString().getBytes("utf-8"));
os.close();
//获取该动态链接响应的状态码
HttpResult = urlconn.getResponseCode();
//判断该动态链接的响应是否能正确连接
if (HttpResult != HttpURLConnection.HTTP_OK) {
return false;
} else {
return true;
}
}
}
分享到:
相关推荐
使用HttpURLConnection访问web页面[文].pdf
tomcat与web程序结构与Http协议与HttpUrlConnection[参考].pdf
基于HttpURLConnection的网络访问 已封装好,一句话访问网络 可采用GET和POST两种方式访问
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();// 取得连接 conn.connect(); InputStream is = conn.getInputStream();//取得返回的InputStream bitmap = BitmapFactory....
正文:我在使用HttpURLConnection.getResponseCode()的时候直接报错是IOException错误,responseCode = -1。一直想不明白,同一个程序我调用了两次,结果有一个链接一直OK,另一个却一直报这个错误。后来发现两个...
彻底解决android开发中,使用用HttpUrlConnection与web服务器之间session保持问题
Android使用HttpURLConnection访问网络
HttpURLConnection用法详解, 轻松掌握HttpURLConnection开发原理
基于HttpURLConnection的网络访问 已封装好,一句话访问网络 可采用GET和POST两种方式访问
Java原生HttpUrlConnection访问WebService实例,附相关jar包...新手整理不喜勿喷
tomcat与web程序结构与Http协议与HttpUrlConnection.pdf
【Java】java.net.HttpURLConnection的使用
一、 HttpURLConnection以GET方式访问网络: HttpURLConnection connection = null; try { URL url = new URL(https://www.xxx.com/); connection = (HttpURLConnection) url.openConnection(); connection....
Java自带的HttpURLConnection访问接口文件上传, 含普通参数值传递和文件传递
//默认的用GET提交 hc.setDoOutput(true); hc.connect(); Map map = hc.getHeaderFields(); //得到Cookie的所有内容,包括SESSIONID,在进行下次提交的时候 直接把这个Cookie的值设到头里头就行了 //...
HttpURLConnection文件下载\httpURLConnection文件下载
android客户端上传数据中文乱码3中解决方案 一种HttpPost两种HTTPURLCONNECTION
HttpURLConnection请求和HttpClient请求...................................................................
调用URL对象的openConnection( )来获取HttpURLConnection对象实例: HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 设置HTTP请求使用的方法:conn.setRequestMethod("GET"); 设置连接超时,...
httpurlconnection, http post, http get