java 发送http请求以及请求参数值出现乱码

一、发送Http POST请求

        public static String sendPost(String apiurl){
        
        String inputline = "";
        // 创建url对象
        URL url = null;
        
        HttpURLConnection connection = null;
        BufferedReader in =null;
        
        try{
            
            url = new URL(apiurl);
            
            // 打开url连接
             connection = (HttpURLConnection) url.openConnection();
    
            // 设置url请求方式 ‘get’ 或者 ‘post’
            connection.setRequestMethod("POST");
    
            // 发送
             in = new BufferedReader(new InputStreamReader(url.openStream()));
    
            // 返回发送结果
             inputline = in.readLine();

        } catch(IOException e) {
            logger.error("", e);
        } finally{
            try{
                if(in != null)
                    in.close();
                if(connection != null)
                    connection.disconnect();
            } catch(final Exception e){
                logger.error("", e);
            }
            return inputline;
        }

==============================================================================

/**
     * 获取字符串的编码格式
     * @param str
     * @return
     */
    public static String getEncoding(String str) {  
        String encode = "GB2312";  
        try {  
            if (str.equals(new String(str.getBytes(encode), encode))) {  
                String s = encode;  
                return s;  
            }  
        } catch (Exception exception) {  
        }  
        encode = "ISO-8859-1";  
        try {  
            if (str.equals(new String(str.getBytes(encode), encode))) {  
                String s1 = encode;  
                return s1;  
            }  
        } catch (Exception exception1) {  
        }  
        encode = "UTF-8";  
        try {  
            if (str.equals(new String(str.getBytes(encode), encode))) {  
                String s2 = encode;  
                return s2;  
            }  
        } catch (Exception exception2) {  
        }  
        encode = "GBK";  
        try {  
            if (str.equals(new String(str.getBytes(encode), encode))) {  
                String s3 = encode;  
                return s3;  
            }  
        } catch (Exception exception3) {  
        }  
        return "";  
    }

在发送之前获取参数的编码格式是GB2312,但是传递到服务器端获取的确实ISO-8859-1,显示为乱码,找过好多方法解决,都没有解决,最终在网上看到一篇文章帮忙解决了

就是在服务器端接收参数的时候将参数转一下码就OK了

现在和大家一起分享一下

package com.zuidaima.util;

02  
03 import java.io.UnsupportedEncodingException;
04  
05 public class 是否为中文检测 {
06  
07     /** 
08      *  用getBytes(encoding):返回字符串的一个byte数组 
09      *  当b[0]为  63时,应该是转码错误 
10      *  A、不乱码的汉字字符串: 
11      *  1、encoding用UTF8时,每byte是负数; 
12      *  2、encoding用ISO8859_1时,b[i]全是63。 
13      *  B、乱码的汉字字符串: 
14      *  1、encoding用ISO8859_1时,每byte也是负数; 
15      *  2、encoding用UTF8时,b[i]大部分是63。 
16      *  C、英文字符串 
17      *  1、encoding用ISO8859_1和UTF8时,每byte都大于0; 
18      *  总结:给定一个字符串,用getBytes("iso8859_1") 
19      *  1、如果b[i]有63,不用转码;  A-2 
20      *  2、如果b[i]全大于0,那么为英文字符串,不用转码;  B-1 
21      *  3、如果b[i]有小于0的,那么已经乱码,要转码。  C-1 
22      */
23     private static String toUTF8(String str) {
24         if (str == null)
25             return null;
26         String retStr = str;
27         byte b[];
28         try {
29             b = str.getBytes("ISO8859_1");
30             for (int i = 0; i < b.length; i++) {
31                 byte b1 = b[i];
32                 if (b1 == 63)
33                     break; // 1
34                 else if (b1 > 0)
35                     continue;// 2
36                 else if (b1 < 0) { // 不可能为0,0为字符串结束符
37                     // 小于0乱码
38                     retStr = new String(b, "UTF8");
39                     break;
40                 }
41             }
42         } catch (UnsupportedEncodingException e) {
43             // e.printStackTrace();
44         }
45         return retStr;
46     }
47  
48     public static void main(String[] args) throws UnsupportedEncodingException {
49         byte[] a = new byte[] { -61, -90, -62, -100, -62, -128, -61, -92, -62,
50                 -69, -62, -93, -61, -89, -62, -96, -62, -127 };
51  
52         String b = new String(a, "utf-8");
53         System.out.println(b);
54         System.out.println(b + " 转换 " + toUTF8(b));
55  
56         String c = new String(b.getBytes("ISO8859-1"), "utf-8");
57         System.out.println(c);
58         System.out.println(c + " 转换 " + toUTF8(c));
59  
60         String d = "最代码";
61         System.out.println(d + " 转换 " + toUTF8(d));
62  
63         printArray(d.getBytes());
64  
65         String e = "最代码网";
66         System.out.println(e + " 转换 " + toUTF8(e));
67  
68         printArray(e.getBytes());
69  
70         String f = "《》";
71         System.out.println(f + " 转换 " + toUTF8(f));
72  
73         String g = "¥";
74         System.out.println(g + " 转换 " + toUTF8(g));
75  
76         String h = "abcedf1234<>d?";
77         System.out.println(h + " 转换 " + toUTF8(h));
78  
79         String i = "やめて";
80  
81         System.out.println(i + " 转换 " + toUTF8(i));
82  
83         String j = new String(e.getBytes("utf-8"), "iso8859-1");
84         System.out.println(j);
85         printArray(j.getBytes());
86  
87     }
88  
89     public static void printArray(byte[] bs) {
90         System.out.println("----");
91         for (byte _bs : bs) {
92             System.out.print(_bs + ",");
93         }
94         System.out.println("\n----");
95     }
96 }

参考链接:http://www.zuidaima.com/share/2310597642324992.htm

你可能感兴趣的:(java)