• 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中会报错 }
  • 相关阅读:
    MongoDB的安装和常用命令
    mysql安装、使用与遇见的问题汇总
    devicePixelRatio,Viewport,移动端适配
    javascript 数组以及对象的深拷贝(复制数组或复制对象)的方法
    正则表达式
    npm 常用命令
    Markdown 基本语法
    mysql忘记root密码
    mysql5.7.12/13在安装新实例时报错:InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero
    mysqld数据位于a盘,执行delete from table, 发现另外2个盘磁盘使用率接近100%,而a盘的使用率反而很低,y??
  • 原文地址:https://www.cnblogs.com/cs-lcy/p/7327515.html
Copyright © 2020-2023  润新知