restful服务接口访问乱码 和 505错误

用cxf 发部个rest服务,用浏览器访问和 HttpURLConnection 访问。


1. URL中有中文,浏览器访问正常,HttpURLConnection 失败。

解决: HttpURLConnection 方式需要做兼容处理。

queryParam 传入参数,服务实现方法中要处理,如果是乱码要转换,如果中文直接查询

if (!isChineseChar(queryParam))
 {
      queryParam = new String(queryParam.getBytes("iso8859-1"), "utf-8");
 }

// 判断中文
public static boolean isChineseChar(String str)
    {
        boolean temp = false;
        Pattern p=Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m=p.matcher(str);
        if(m.find()){
            temp =  true;
        }
        return temp;
    }

2. HttpURLConnection 请求中 参数中如果有  空格,请求则会 505错误

解决: 需要对有空格的参数 做URL编码处理。

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import sun.net.www.protocol.http.HttpURLConnection;

import com.alibaba.fastjson.JSONObject;

public class SingleTableRestClient 
{
    private static final String targetURL = "http://localhost:8080/agd-restful/services/restful/QueryService/queryData/*?queryParam=";
    
    public static void main(String[] args) 
    {
        JSONObject obj = new JSONObject();
        obj.put("XM", "匡匡");
        obj.put("BIRTHDAY", <span style="color:#FF6666;">getURLEncoder</span>("1988-01-01 00:00:00,1988-12-30 00:00:00"));
        
        String urls = targetURL + obj.toString();
        requestRestServer(urls);
            
    }
    
    public static JSONObject requestRestServer(String url)
    {
        JSONObject obj = new JSONObject();
         try 
         {
             URL restServiceURL = new URL(url);

             HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
             httpConnection.setRequestMethod("GET");
             httpConnection.setRequestProperty("Accept", "application/json");
             httpConnection.setRequestProperty("Accept-Charset", "UTF-8");
             httpConnection.setRequestProperty("contentType", "UTF-8");
            
             if (httpConnection.getResponseCode() != 200) {
                    throw new RuntimeException("HTTP GET Request Failed with Error code : "
                                  + httpConnection.getResponseCode());
             }
            
             BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream()),"utf-8"));

             String output = "";
             String result = "";
             System.out.println("Output from Server:  \n");

             while ((output = responseBuffer.readLine()) != null) {
                    //System.out.println(output);
                    result = output;
             }
             obj = JSONObject.parseObject(result);
             System.out.println(obj.toString());
             httpConnection.disconnect();

        } catch (MalformedURLException e) {

             e.printStackTrace();

        } catch (IOException e) {
             e.printStackTrace();

        }
         return obj;
    }
    
    @SuppressWarnings("deprecation")
    <span style="color:#FF6666;">private static String getURLEncoder(String dest)
    {
        return URLEncoder.encode(dest);
    }</span>
}
修改后  正常ok

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值