restful服務介面訪問亂碼 和 505錯誤,restful505

來源:互聯網
上載者:User

restful服務介面訪問亂碼 和 505錯誤,restful505

用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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.