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. 【转载】linux Jumpserver跳板机堡垒机部署安装使用教程

    原文地址:https://idc.wanyunshuju.com/li/554.html

  2. 数学软件Matlab的使用感受

    在我一年前的暑假,我们的小学期学习了MATLAB软件.MATLAB是一款数学软件,可以用于算法计算.数据可视化.数据分析以及数据计算. 我们主要学习了MATLAB关于数学上的经常用的一些用法和算法,M ...

  3. ubuntu下修改文件权限

    参考:https://www.cnblogs.com/viviwind/archive/2012/08/02/2619451.html 常用方法如下: sudo chmod 600 ××× (只有所有 ...

  4. bootstrap-select 下拉多选组件

    <div class="form-group"> <label class="col-lg-2 col-sm-2 control-label" ...

  5. maven包上传私服

    选择需要上传的项目右键-->Run As-->Run Configurations-->Maven Buid-->右键 new -->选择 base directory- ...

  6. Aizu - 2681(括号匹配)

    Problem Statement You are given nn strings str1,str2,…,strnstr1,str2,…,strn, each consisting of ( an ...

  7. leetcode第40题:组合总和II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  8. 启动和停止mysql的正确姿势

    1.如果是用脚本起的.那就用脚本停 2.最好用mysql_safe起,mysqladmin -uroot -p shutdown -S /tmp/mysql.sock停 mysqld_safe --d ...

  9. __new__() 与__init__()的区别

    __new__作用于__init__之前.前者可以决定是否调用后者,或者说可以决定调用那个类的__init__方法. 首先要知道在面向对象编程中,实例化基本遵循创建实例对象,初始化实例对象,最后返回实 ...

  10. 从输入URL到页面加载发生了什么?

    总体来说分为以下几个过程: DNS解析 TCP连接 发送HTTP请求 服务器处理请求并返回HTTP报文 浏览器解析渲染页面 连接结束 具体内容点这里