baggio1024 发表于 2013-1-28 19:27:04

用HttpURLConnection访问web页面.中文乱码的解决

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 blocke.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;}   }}
页: [1]
查看完整版本: 用HttpURLConnection访问web页面.中文乱码的解决