HttpClient-传入url得到json字符串( PostMethod method = new PostMethod(url)是个好方法)

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import net.sf.json.JSONObject; @Controller
@RequestMapping("/synchronize")
public class SynchronizeController {
private Log log = LogFactory.getLog(BuildNavigatorServiceImpl.class); private String requestIP = ""; private int requestPort; /**
* 用户名
*/
private String user = ""; /**
* 密码
*/
private String password = ""; /*rest请求的客户端*/
HttpClient client = null;
private HttpClient getClient(){
if(client!=null){
return client;
}
HttpClient client = new HttpClient();
client.getState().setCredentials(new AuthScope(requestIP, requestPort, null), new UsernamePasswordCredentials(user, password));
client.getHttpConnectionManager().getParams().setConnectionTimeout(1000*60*5);
client.getHttpConnectionManager().getParams().setSoTimeout(1000*60*5);
this.client = client;
return client;
} /*传入刘德鹤的url 就可以得到json字符串*/
  String  url = "http://1000.1992.2008.118:8080/adt/rest/data/getFillTabColumnsInfo?
dbId=g5c5b886ed5c4de7826694bbb6025950&tableNm=yx_c_cust"; /*只有这个方法有用传入url然后得到json*/
@RequestMapping("restful")
private void restfulReq(HttpServletRequest request, HttpServletResponse response) throws Exception{
String url = request.getParameter("url");
String res = "";
HttpClient client = getClient(); //或者是直接new HttpClient
PostMethod method = new PostMethod(url); //改成post,以前是GetMethod

method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
method.setDoAuthentication(true);
try {
int result = client.executeMethod(method);
        if(result != 200) {
throw new Exception("访问Navigator失败,错误码:" + String.valueOf(result));
}
// log.info("访问Navigator,获取数据完成!");
res = method.getResponseBodyAsString();
System.out.println(res);
} catch (HttpException e) {
// log.error("访问Navigator失败!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally { //
     method.releaseConnection();
} /*将res返回前台*/
response.setContentType("application/json;charset=utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} out.print(res.toString()); // return res; 必须注释掉,否则在ajax中的success中会报错
}
上一篇:关于PYTHON_EGG_CACHE无权限的问题


下一篇:Appium 解决锁屏截屏问题(java篇)