当前位置:   article > 正文

restful服务接口访问乱码 和 505错误_aws 文件url有空格 505

aws 文件url有空格 505

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


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

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

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

  1. if (!isChineseChar(queryParam))
  2. {
  3. queryParam = new String(queryParam.getBytes("iso8859-1"), "utf-8");
  4. }
  5. // 判断中文
  6. public static boolean isChineseChar(String str)
  7.     {
  8.         boolean temp = false;
  9.         Pattern p=Pattern.compile("[\u4e00-\u9fa5]");
  10.         Matcher m=p.matcher(str);
  11.         if(m.find()){
  12.             temp =  true;
  13.         }
  14.         return temp;
  15.     }

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

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

  1.  import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.net.URLEncoder;
  7. import sun.net.www.protocol.http.HttpURLConnection;
  8. import com.alibaba.fastjson.JSONObject;
  9. public class SingleTableRestClient
  10. {
  11.     private static final String targetURL = "http://localhost:8080/agd-restful/services/restful/QueryService/queryData/*?queryParam=";
  12.     
  13.     public static void main(String[] args)
  14.     {
  15.         JSONObject obj = new JSONObject();
  16.         obj.put("XM", "匡匡");
  17.   obj.put("BIRTHDAY", <span style="color:#FF6666;">getURLEncoder</span>("1988-01-01 00:00:00,1988-12-30 00:00:00"));
  18.         
  19.         String urls = targetURL + obj.toString();
  20.         requestRestServer(urls);
  21.             
  22.     }
  23.     
  24.     public static JSONObject requestRestServer(String url)
  25.     {
  26.         JSONObject obj = new JSONObject();
  27.          try
  28.          {
  29.              URL restServiceURL = new URL(url);
  30.              HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
  31.              httpConnection.setRequestMethod("GET");
  32.              httpConnection.setRequestProperty("Accept", "application/json");
  33.              httpConnection.setRequestProperty("Accept-Charset", "UTF-8");
  34.              httpConnection.setRequestProperty("contentType", "UTF-8");
  35.             
  36.              if (httpConnection.getResponseCode() != 200) {
  37.                     throw new RuntimeException("HTTP GET Request Failed with Error code : "
  38.                                   + httpConnection.getResponseCode());
  39.              }
  40.             
  41.              BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
  42.                     (httpConnection.getInputStream()),"utf-8"));
  43.              String output = "";
  44.              String result = "";
  45.              System.out.println("Output from Server:  \n");
  46.              while ((output = responseBuffer.readLine()) != null) {
  47.                     //System.out.println(output);
  48.                     result = output;
  49.              }
  50.              obj = JSONObject.parseObject(result);
  51.              System.out.println(obj.toString());
  52.              httpConnection.disconnect();
  53.         } catch (MalformedURLException e) {
  54.              e.printStackTrace();
  55.         } catch (IOException e) {
  56.              e.printStackTrace();
  57.         }
  58.          return obj;
  59.     }
  60.     
  61.     @SuppressWarnings("deprecation")
  62.     <span style="color:#FF6666;">private static String getURLEncoder(String dest)
  63.     {
  64.         return URLEncoder.encode(dest);
  65.     }</span>
  66. }
修改后  正常ok

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/639765
推荐阅读
相关标签
  

闽ICP备14008679号