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. 每天CSS学习之text-align

    text-align是CSS的一个属性,其作用是设置文本的对齐方式.其值如下所示: 1.left:文本左对齐.如下所示: div{ text-align:left; } 结果: 2.right:文本右 ...

  2. Linux学习 :移植U-boot_2012.04.01到JZ2440开发板

    一.下载U-boot源码:ftp://ftp.denx.de/pub/u-boot/ 二.uboot的启动过程: 部分硬件初始化——>加载完整uboot到RAM——>跳转到第二阶段入口开始 ...

  3. 5.2 C++重载操作符的优先级

    参考:http://www.weixueyuan.net/view/6380.html 总结: 重载操作符不能改变操作符的优先级和语法特性. 重载操作符不能改变操作符的优先级和语法特性.例如上一节复数 ...

  4. CentOS7安装配置Amanda

    参考: https://wenku.baidu.com/view/881e0c998e9951e79a892759.html yum  -y  install  amanda* http://blog ...

  5. DevExpress ASP.NET v18.2新功能详解(三)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Cont ...

  6. 一次练习 发现的问题,malloc free 无效;findfirstfile失败,writefile失败的原因

    #include <windows.h> #include <stdio.h> #include <shlwapi.h> #pragma comment(lib,& ...

  7. C#中三层架构UI、BLL、DAL、Model实际操作

    三层架构分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL)再加上实体类库(Model) 转载请注明出自朱朱家园https://blog.csdn.net/zhgl7688 1.实体类库( ...

  8. 2019-02-25 EST 科技文翻译

    The Definition of Theme and Rheme The point of departure is equally presented to the speaker and to ...

  9. linux下关于PCL(point cloud library)库的安装,三行命令错误的问题

    最近想再看看PCL,所以进行了安装,在之前的接触的过程中,由于之前的网络存在问题,导致以下三个命令: sudo add-apt-repository ppa:v-launchpad-jochen-sp ...

  10. Python基础4--一看就会的选择与循环

    1 选择 if elif else 注意后面均有: if age>18: print 'adult' elif age>6: print 'teenager' else: print 'k ...