1. 程式人生 > >java 傳送http請求以及請求引數值出現亂碼

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