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中会报错
}

HttpClient-传入url得到json字符串( PostMethod method = new PostMethod(url)是个好方法)的更多相关文章

  1. httpclient的调用 发送json字符串

    public static String postHttp(JSONObject jsonObject, String jsonUrl){ String responseMsg="" ...

  2. fastjson将json字符串转化成bean对象解析出错的检查方法

    我的情况是:解析第一层数据成功,解析第二层嵌套的数据失败.如: { "response": { "resultcode": "0", &qu ...

  3. spring接收json字符串的两种方式

    一.前言 前几天遇到一个问题,前端H5调用我的springboot一个接口(post方式,@RequestParameter接收参数),传入的参数接收不到.自己测试接口时使用postman的form- ...

  4. DataTable 和Json 字符串互转

    #region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  5. Objective-C-使用NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串

    NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...

  6. Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mengzhengbin520.blog.51cto.com/7590564/12 ...

  7. Objective-C——NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串

    NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...

  8. 使用jackson来进行数组格式的json字符串转换成List。

    有一个字符串如下.如下,也是通过jackson把list转换成的json字符串,我想把它转过来,看网上的内容都不尽人如意,都是片断的内容.估计只有写的知道怎么使用,所以就直接看了jackson的官网, ...

  9. c# 自定义解析JSON字符串数据

    解析json字符串有很多方式, 1 : 在网上下载json解析的dll类库并添加引用, 调用相关方法; 2 : 使用自带类库JavaScriptSerializer的序列号和反序列化; 对于以上两个方 ...

随机推荐

  1. 【资料收集】QT 环境安装配置

    (很详细,极力推荐) [OpenCV] -- win7下配置OpenCV的Qt开发环境 - 代码人生 - 博客频道 - CSDN.NET  http://blog.csdn.net/qiurisuix ...

  2. 深入研究sqlalchemy连接池

    简介: 相对于最新的MySQL5.6,MariaDB在性能.功能.管理.NoSQL扩展方面包含了更丰富的特性.比如微秒的支持.线程池.子查询优化.组提交.进度报告等. 本文就主要探索MariaDB当中 ...

  3. Java面向对象的三大特性之一 多态

    多态: 子类重写父类方法 1)位置:子类和父类中有同名的方法 2)方法名相同,返回类型和修饰符相同,参数列表相同       方法体不同 多态的优势和应用场合 多态:同一个引用类型,使用不同的实例而执 ...

  4. substr、substring和slice的区别

    substr(start,[length])表示从start位置开始取length个字符串:substring(start,end)表示从start,到end之间的字符串,包括start位置的字符但是 ...

  5. [PyImageSearch] Ubuntu16.04 使用OpenCV和python识别信用卡 OCR

    在今天的博文中,我将演示如何使用模板匹配作为OCR的一种形式来帮助我们创建一个自动识别信用卡并从图像中提取相关信用卡数位的解决方案. 今天的博文分为三部分. 在第一部分中,我们将讨论OCR-A字体,这 ...

  6. Java基础-类和对象

    类和对象 对象:对象是类的一个实例(对象不是找个女朋友),有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行为有:摇尾巴.叫.吃等. 类:类是一个模板,它描述一类对象的行为和状态. ...

  7. SharePoint Framework 构建你的第一个web部件(一)

    博客地址:http://blog.csdn.net/FoxDave SharePoint客户端web部件是出现在SharePoint页面的控件,但却是在浏览器本地运行的.他们是SharePoint ...

  8. Android UIAutomator 定位

    AndroidUIAutomator:Android的源生测试框架的定位方式,定位速度快 一.组合定位 1.一般组合用id,class,text这三个属性会比较好一点,但也可以组合定位 2.id与te ...

  9. alter system archive log current作用及和alter system switch logfile区别

    alter system archive log current 是归档当前的重做日志文件,不管自动归档有没有打都归档. alter system switch logfile 是强制日志切换,不一定 ...

  10. ubuntu安装nodejs,npm live-server

    sudo apt-get install curl 先安装的是curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/in ...