一、发送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;
03 | import java.io.UnsupportedEncodingException; |
08 | * 用getBytes(encoding):返回字符串的一个byte数组 |
11 | * 1、encoding用UTF8时,每byte是负数; |
12 | * 2、encoding用ISO8859_1时,b[i]全是63。 |
14 | * 1、encoding用ISO8859_1时,每byte也是负数; |
15 | * 2、encoding用UTF8时,b[i]大部分是63。 |
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 |
23 | private static String toUTF8(String str) { |
29 | b = str.getBytes( "ISO8859_1" ); |
30 | for ( int i = 0 ; i < b.length; i++) { |
38 | retStr = new String(b, "UTF8" ); |
42 | } catch (UnsupportedEncodingException e) { |
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 }; |
52 | String b = new String(a, "utf-8" ); |
53 | System.out.println(b); |
54 | System.out.println(b + " 转换 " + toUTF8(b)); |
56 | String c = new String(b.getBytes( "ISO8859-1" ), "utf-8" ); |
57 | System.out.println(c); |
58 | System.out.println(c + " 转换 " + toUTF8(c)); |
60 | String d = "最代ç " ; |
61 | System.out.println(d + " 转换 " + toUTF8(d)); |
63 | printArray(d.getBytes()); |
66 | System.out.println(e + " 转换 " + toUTF8(e)); |
68 | printArray(e.getBytes()); |
71 | System.out.println(f + " 转换 " + toUTF8(f)); |
74 | System.out.println(g + " 转换 " + toUTF8(g)); |
76 | String h = "abcedf1234<>d?" ; |
77 | System.out.println(h + " 转换 " + toUTF8(h)); |
81 | System.out.println(i + " 转换 " + toUTF8(i)); |
83 | String j = new String(e.getBytes( "utf-8" ), "iso8859-1" ); |
84 | System.out.println(j); |
85 | printArray(j.getBytes()); |
89 | public static void printArray( byte [] bs) { |
90 | System.out.println( "----" ); |
92 | System.out.print(_bs + "," ); |
94 | System.out.println( "\n----" ); |
参考链接:http://www.zuidaima.com/share/2310597642324992.htm