import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import com.zrar.system.Log;
public class HttpClientGetJson {
public static final String CHARSET = "UTF-8";
public String getJsonFromWs(String url) {
//String retJsonStr = "";
StringBuffer sbf = new StringBuffer();
HttpClient httpClient = new HttpClient();
GetMethod method = new GetMethod(url);
try{
/*method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");*/
Log.print("开始连接WEBSERVICE服务端!");
httpClient.executeMethod(method);
//retJsonStr = method.getResponseBodyAsString();
InputStream ins = method.getResponseBodyAsStream();
//转换编码
BufferedReader br = new BufferedReader(new InputStreamReader(ins,CHARSET));
String line = null;
while ((line = br.readLine()) != null)
{
sbf.append(line);
}
br.close();
/*System.out.println(sbf.toString());*/
Log.print("结束WEBSERVICE服务端连接!");
}catch(Exception e){
e.printStackTrace();
Log.print("从WEBSERVICE服务端获取json格式数据字符串错误!");
}finally{
//关闭连接
method.releaseConnection();
httpClient.getHttpConnectionManager().closeIdleConnections(0);
}
return sbf.toString();
}
}
httpclien调用webservice_hanCSDN_20130427
最新推荐文章于 2024-07-19 16:48:37 发布