引入的包是:commons-codec-1.5.jar、commons-httpclient-3.0.1.jar、httpcore-4.2.4.jar
HttpClient client = new HttpClient();
String url="http://localhost:8089/public/getNameLists?entpName="+entpName+ "&orgCode=" +orgCode+ "&typeCode=" +typeCode+ "";
System.out.println("連接的url資料:"+url);
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); //連接超時為5秒
client.getHttpConnectionManager().getParams().setSoTimeout(5000); //讀取超時
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("Content-Type", "application/json");
//設定引數編碼為utf-8
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
try {
//讀取內容
int status = client.executeMethod(postMethod);
if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream()));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while((str = reader.readLine())!=null){
stringBuffer.append(str);
}
String html=stringBuffer.toString();
System.out.println("回傳來的資料:"+html);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//釋放連接
postMethod.releaseConnection();
}
列印結果:回傳來的資料:{"result":"1","tableName":"重大稅收違法案件,"}
正確的結果:{"result":"1","tableName":"重大稅收違法案件,工商吊銷企業名單,"} tableName中缺少一個值,請問是怎么回事兒,因為專案中好多地方都用了這個包,所以不想換掉,請問如果不更換jar包 還有沒有別的解決辦法。。
uj5u.com熱心網友回復:
重大稅收違法案件,工商吊銷企業名單 兩句話中間有沒有空格 換行之類的,.readLine()讀取到空格換行就會停止。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/228148.html
標籤:Java SE
上一篇:阿里云物聯網拉取資料的類如何啟動
