现在项目用要用HttpURLConnection访问另一个服务器的服务获取一点数据,并需要在url中传递参数,参数会有中文.现在传中文的时候会有问题,现在我是这样实现的                        URL url = new URL(url);  
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();  
urlConn.setDoOutput(true); 
urlConn.setUseCaches(false);  
urlConn.setRequestProperty("Content-type","application/x-www-form-urlencoded");  
urlConn.setRequestMethod("POST");  
urlConn.connect();
DataOutputStream os = new DataOutputStream(urlConn.getOutputStream());  
                       //问题就在这,如何处理这个中文参数呢,encode后的编码方式怎么确定呢?比如是对方服务器的编码方式,还是对方数据库的的编码方式,或者是还有别的访问方式呢?
String param = "param="+URLEncoder.encode("中文参数","UTF-8")
os.write(param.getBytes());
os.flush();
os.close();
InputStream inStrm = urlConn.getInputStream();
int ch;
StringBuffer fu=new StringBuffer();
while ((ch = inStrm.read()) != -1) {
fu.append((char)ch);
}
System.out.println(fu);

解决方案 »

  1.   

    我说一种思路哈。  1、URL中增加一个编码方式的参数,例如?param=中文参数&enc=utf-8,(enc就是你的param参数值的编码方式)然后和接收端协议:按照你的编码方式进行接收端的有效decode转码工作。
    public static void main(String[] args) {
    try {
    //模拟客户端的编码方式
    String encode = "UTF-8";
    String enc = URLEncoder.encode("http://www.baidu.com?tag=中文参数&enc=" + encode, encode);
    System.out.println(enc);

    //模拟服务端的转码方式
    String dec = URLDecoder.decode(enc, encode);
    System.out.println(dec);
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    }打印结果是
    http%3A%2F%2Fwww.baidu.com%3Ftag%3D%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0%26enc%3DUTF-8
    http://www.baidu.com?tag=中文参数&enc=UTF-8
      

  2.   

     要保证服务端的servlet没有经过设置编码的filter,否则容易乱。
      

  3.   

    URLEncoder.encode(para)  改成  URLEncoder.encode(para,"?") 
    后面增加一个参数,里面是字符集,可以是utf-8、gbk等等,你可以尝试一下,应该总有一个可以的。取决于web服务器采用的是什么字符集编码。
      

  4.   

    public class UrlConnection {
    static HttpURLConnection httpurlconnection = null; /**
     * 发送请求
     * 
     * @throws Exception
     */
    public static void sendReques() throws Exception { URL url = null;
    url = new URL("http://192.168.1.126:8080/demo/LoginServlet");
    httpurlconnection = (HttpURLConnection) url.openConnection();
    httpurlconnection.setDoOutput(true);
    httpurlconnection.setRequestMethod("POST");
    // 转码为utf-8格式
    String u = URLEncoder.encode("中文字符串","utf-8");
    String username = "username=" + u;
    httpurlconnection.getOutputStream().write                (username.getBytes());
    httpurlconnection.getOutputStream().flush();
    httpurlconnection.getOutputStream().close();
    }这段代码是我们项目经常用的方法,用post方式传递参数,从来没出现过乱码问题,这个utf-8是调用者与被调用者约定的一个编码个式,这里用了utf-8转码,那么被调用方解析的时候就要用这种方式来转码,跟其它因素都无关这里有我上传的完整例子,可以看一下:
    http://download.csdn.net/source/2692145
      

  5.   

    用base64加密下,让对方用base64获取的时候解密下就ok了。
      

  6.   

    public class UrlConnection {
    static HttpURLConnection httpurlconnection = null;/**
    * 发送请求

    * @throws Exception
    */
    public static void sendReques() throws Exception {URL url = null;
    url = new URL("http://192.168.1.126:8080/demo/LoginServlet");
    httpurlconnection = (HttpURLConnection) url.openConnection();
    httpurlconnection.setDoOutput(true);
    httpurlconnection.setRequestMethod("POST");
    // 转码为utf-8格式
    String u = URLEncoder.encode("中文字符串","utf-8");
    String username = "username=" + u;
    httpurlconnection.getOutputStream().write  (username.getBytes());
    httpurlconnection.getOutputStream().flush();
    httpurlconnection.getOutputStream().close();
    }这段代码是我们项目经常用的方法,用post方式传递参数,从来没出现过乱码问题,这个utf-8是调用者与被调用者约定的一个编码个式,这里用了utf-8转码,那么被调用方解析的时候就要用这种方式来转码,跟其它因素都无关这里有我上传的完整例子,可以看一下:
    http://download.csdn.net/source/2692145
      

  7.   

    1、双方约定
    2、从head里取出来试试