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是负数; 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值