(1)使用JavaScript对传递的参数使用encodeURI()函数来对进行UTF-8字符编码
<script>
var str="参数乱码";
window.location.href="index.htm?a="+encodeURI(str);
</script>
(2)用Java实现
import java.io.*;
import java.net.*;
public class PostExample
{
public static void main(String[] argv) throws Exception
{
URL URL = new URL(http://www.Javacourses.com/cgi-bin/names.cgi);
HttpURLConnection connection = (HttpURLConnection)URL.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
PrintWriter out = new.PrintWriter(connection.getOutputStream());
String name = "name="+URLEncoder.encode("Qusay Mahmoud","UTF-8");
String email = "email="+URLEncoder.encode(qmahmoud@Javacourse.com,"UTF-8");
//URLEncoder.encode方法对消息进行编码,事后用URLDecoder.decode方法进行解码,
out.print(name+"&"+email);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = in.readLine())!=null){
System.out.println(line);
}
in.close();
}
}
Url地址栏中让传递参数变为乱码(很实用!!!)
最新推荐文章于 2022-09-06 19:50:25 发布