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中會報錯 }
相關文章
相關標籤/搜索