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中会报错
}
HttpClient-传入url得到json字符串( PostMethod method = new PostMethod(url)是个好方法)的更多相关文章
- httpclient的调用 发送json字符串
public static String postHttp(JSONObject jsonObject, String jsonUrl){ String responseMsg="" ...
- fastjson将json字符串转化成bean对象解析出错的检查方法
我的情况是:解析第一层数据成功,解析第二层嵌套的数据失败.如: { "response": { "resultcode": "0", &qu ...
- spring接收json字符串的两种方式
一.前言 前几天遇到一个问题,前端H5调用我的springboot一个接口(post方式,@RequestParameter接收参数),传入的参数接收不到.自己测试接口时使用postman的form- ...
- DataTable 和Json 字符串互转
#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...
- Objective-C-使用NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串
NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...
- Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mengzhengbin520.blog.51cto.com/7590564/12 ...
- Objective-C——NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串
NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...
- 使用jackson来进行数组格式的json字符串转换成List。
有一个字符串如下.如下,也是通过jackson把list转换成的json字符串,我想把它转过来,看网上的内容都不尽人如意,都是片断的内容.估计只有写的知道怎么使用,所以就直接看了jackson的官网, ...
- c# 自定义解析JSON字符串数据
解析json字符串有很多方式, 1 : 在网上下载json解析的dll类库并添加引用, 调用相关方法; 2 : 使用自带类库JavaScriptSerializer的序列号和反序列化; 对于以上两个方 ...
随机推荐
- CSS(三)--自定义标签
HTML代码 <body> <ul> <li>1</li> <li>2</li> </ul> </body&g ...
- flask使用配置文件
引入配置 app = Flask(__name__) app.config.from_pyfile('config.py') config.py DEBUG = True SECRET_KEY = '
- 【重大更新】DevExpress WinForms v18.2新版亮点(七)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...
- mybatis column 和property
mybatis map文件中 resultMap中column和sql查询结果对应, property和实体private对应 <resultMap id="VideoYcAppRes ...
- python中变量命名的基本规则,标识符和关键字
变量的命名 目标 标识符和关键字 变量的命名规则 0.1 标识符和关键字 1.1 标识符 标示符就是程序员定义的 变量名.函数名 名字 需要有 见名知义 的效果,见下图:  标示符可以由 字母.下划 ...
- SharePoint REST API - OData查询操作
博客地址:http://blog.csdn.net/FoxDave 本篇主要讲述SharePoint REST中OData的查询操作.SharePoint REST服务支持很多OData查询字符串 ...
- android-DNS服务找不到
1.重启eclipse 2.重新建立AVD 3.在建立AVD时sd卡数值不要填
- shelly - HYMN TO INTELLECTUAL BEAUTY
HYMN TO INTELLECTUAL BEAUTY III No voice from some sublimer world hath ever To sage or poet these r ...
- mysql cast
之前讲到了orcale的字符串与日期等类型的转换,现在我们来看看Mysql是怎么转换的.比起orcale,MySQL相比之下就简单得多了,只需要一个Cast()函数就能搞定.其语法为:Cast(字段名 ...
- python Django rest-framework 创建序列化工程步骤
11创建项目 2创建应用 3stting添加应用(apps)-添加制定数据库-修改显示汉字(zh-hans)-上海时区(Asia/Shanghai) 4主路由添加子路由 5应用里创建子路由 6创建数据 ...