Java,URL输出在Java输出中不同
我有一个关于URI和URL的问题 当我通过一个网址是好的工作,但结果是最坏的需要帮助 因为我的代码是这样的Java,URL输出在Java输出中不同,java,url,unicode,utf-8,uri,Java,Url,Unicode,Utf 8,Uri,我有一个关于URI和URL的问题 当我通过一个网址是好的工作,但结果是最坏的需要帮助 因为我的代码是这样的 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")
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();
}
}
}
web中的输出不同。。
在我的java输出上是
你的哈达哈达!
但在我看来,这个网站是
ÄÄþþþþþþþþþ
救命
您不需要(也不应该)在Java中执行newstring
-stringuser=“boo”代码>很好
String msg = new String("你的哈达哈达!");
在源代码中写入非ASCII字符意味着您必须将-encoding
标志设置为javac
,以匹配保存文本文件的编码。您可能已将.java文件保存为UTF-8,但未将构建环境配置为在编译时使用UTF-8
如果不确定是否正确,可以同时使用ASCII safe\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
是一个很好的猜测。您可以提供更多关于您在网站上打印此内容的详细信息吗?编码是您的问题,您必须使用另一种接受亚洲字母的编码。。顺便说一句,不要使用新字符串
而只使用“”,psw不应传入获取方法
@raygozag该网站是一个消息服务,当您更改消息时,它将发送到目标号码。@nachokk该密码工作正常,因为它只会在url内进行检查。有办法对亚洲字母进行编码吗?不知道,可以在两个位置,当你通过url检查时myUrl.toasitring()
如果正确,或者当你阅读时可能是编码,你在阅读输入流时必须使用一些中文编码
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)
);