2013-07-09 113 views
0

我有一个关于URI和URL的问题 当我通过一个url是工作好,但结果是最糟糕的需要帮助!Java,URL输出与java输出不同

因为我的代码是这样的。

import java.io.*; 
import java.net.*; 
import java.net.URL; 

public class isms { 
    public static void main(String[] args) throws Exception { 
     try { 


     String user = new String ("boo"); 
     String pass = new String ("boo"); 
     String dstno = new String("60164038811"); //You are going compose a message to this destination number. 
     String msg = new String("你的哈达哈达!"); //Your message over here 
     int type = 2; //for unicode change to 2, normal will the 1. 
     String sendid = new String("isms"); //Malaysia does not support sender id yet. 

      // Send data 
      URI myUrl = new URI("http://www.isms.com.my/isms_send.php?un=" + user + "&pwd=" + pass 
       + "&dstno=" + dstno + "&msg=" + msg + "&type=" + type + "&sendid=" + sendid); 
      URL url = new URL(myUrl.toASCIIString()); 

      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(true); 

      // Get the response 
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      String line; 
      while ((line = rd.readLine()) != null) { 
       // Print the response output... 
       System.out.println(line); 
      }  
      rd.close(); 

      System.out.println(url); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 



    } 
} 

在网络的输出是不同的.. 在我的Java输出

你的哈达哈达!

,但在我的网站是

ÄãμĹþ'ï¹þ'ï!

帮助!!

+0

你能提供更多关于你在网站上打印这些东西的详细信息吗? – raygozag

+3

编码是你的问题,你必须使用另一种编码,接受亚洲字母..顺便说一句,不要使用'新的字符串'而是使用只是“”和psw不应该传入'GET METHOD' – nachokk

+0

@raygozag该网站是一个消息服务,当你改变它将发送到目的地号码的消息。 –

回答

0
String user = new String ("boo"); 

您不需要(也不应该)做new String在Java的String user = "boo";是罚款。

String msg = new String("你的哈达哈达!"); 

在源写非ASCII字符意味着你必须得到-encoding标志javac,以配合您与保存在文本文件的编码。您可能将.java文件保存为UTF-8,但未在编译时将您的构建环境配置为使用UTF-8。

如果你不知道,你有这个权利,你可以在此期间使用ASCII安全\u逃逸:

String msg = "\u4F60\u7684\u54C8\u8FBE\u54C8\u8FBE!"; // 你的哈达哈达! 

最后:

URI myUrl = new URI("http://www.isms.com.my/isms_send.php?un=" + user + "&pwd=" + pass 
      + "&dstno=" + dstno + "&msg=" + msg + "&type=" + type + "&sendid=" + sendid); 

当你把一起使用的URI应该是URL转义字符串中包含的每个参数。否则,值中的任何&或其他无效字符都会中断查询。这也允许你选择用什么字符集创建查询字符串。

String enc = "UTF-8"; 
URI myUrl = new URI("http://www.isms.com.my/isms_send.php?" + 
    "un=" + URLEncoder.encode(user, enc) + 
    "&pwd=" + URLEncoder.encode(pass, enc) + 
    "&dstno=" + URLEncoder.encode(dstno, enc) + 
    "&msg=" + URLEncoder.encode(msg, enc) + 
    "&type=" + URLEncoder.encode(Integer.toString(type), enc) + 
    "&sendid=" + URLEncoder.encode(sendid, enc) 
); 

什么enc正确的价值是取决于你正在连接的服务,但UTF-8是一个很好的猜测。