jenkins 執行可執行jar包測試中,請求亂碼解決辦法

weixin_33896726發表於2017-01-03

自動化指令碼在eclipse中執行,沒有問題。jenkins構建打包自動化指令碼,在執行指令碼時,遇到了指令碼中傳送的請求的引數為亂碼,實現瞭如下一些解決辦法:

 

1、設定作業系統環境JAVA_TOOL_OPTIONS   = -Dfile.encoding=UTF-8

2、jenkins 系統管理> 系統設定 設定全域性變數

3、修改程式碼,程式碼中把請求引數在使用前變成uft8 格式。響應輸出流也專換成utf-8

para = new String(para.toString().getBytes(), "utf-8");   使用引數

 in = new BufferedReader( new InputStreamReader(conn.getInputStream(),"utf-8"));   響應輸出流也專換成utf-8

 
  public static String sendPost(String url, String param,String wexinsession) {
    para = new String(para.toString().getBytes(), "utf-8");  PrintWriter out
= null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 開啟和URL之間的連線 URLConnection conn = realUrl.openConnection(); // 設定通用的請求屬性 addRequestHeader("Content-Type","text/html;charset=UTF-8"); conn.setRequestProperty("Content-Type", "text/html;charset=UTF-8"); conn.setRequestProperty("Host", PropsUtil.readValue("emdsServerHost")); conn.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01"); conn.setRequestProperty("Connection", "keep-alive"); conn.setRequestProperty("X-Requested-With", "XMLHttpRequest"); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); conn.setRequestProperty("Accept-Language", "zh-cn"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Origin", "http://xxx.xxx.xxx); conn.setRequestProperty("Content-Length", "700"); conn.setRequestProperty("Connection", "keep-alive"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13C75 MicroMessenger/6.3.9 NetType/WIFI Language/zh_CN"); conn.setRequestProperty("Referer", "http://xxx.xxx.xxx/sss.ss"); conn.setRequestProperty("Cookie", wexinsession); // 傳送POST請求必須設定如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection物件對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 傳送請求引數 out.print(param); // flush輸出流的緩衝 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream(),"utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } // System.out.println(result); } catch (Exception e) { System.out.println("傳送 POST 請求出現異常!"+e); e.printStackTrace(); } //使用finally塊來關閉輸出流、輸入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; }

 

相關文章