zoukankan      html  css  js  c++  java
  • 使用httpclient发送post请求与get请求

       最近因为项目的要求,需要使用httpclient来发送请求。但是查阅了许多博客,大家发送请求的方法各不相同。原因是因为httpclient的jar包的不同版本,其内部方法也不相同。因此抛开具体用到的jar包而直接复制方法是没有意义的,很容易出现找不到方法的情况。所以在此给出用到的jar包,和在这个jar包下调用的方法。

    发送post请求:

    @Controller
    public class PostController {
    	
    	@RequestMapping(value="request.html")
    	public ModelAndView requestPost(HttpServletRequest request,@RequestParam(value="code")String code)
    	{
    		AuthConfig config = AuthConfig.getInstance();
    		String server = config.getConfigParameter("server");
    		String client_id = config.getConfigParameter("client_id");
    		String client_secret = config.getConfigParameter("client_secret");
    		String redirect_uri = config.getConfigParameter("redirect_uri");
    		String scope = config.getConfigParameter("scope");
    		System.out.println("授权码是:"+code);
    		String url = server+"AuthServer/oauth2/token?client_id="+client_id+"&client_secret="+client_secret+"&grant_type=authorization_code&code="+code+"&redirect_uri="+redirect_uri+"&scope="+scope;
    		System.out.println(url);
    		HttpClient client1 = new HttpClient();  
    	    PostMethod method1 = new PostMethod(url);  
    	    method1.setRequestHeader("Content-Type","application/json; UTF-8");  
    	    //method1.setRequestHeader("Content-Type","text/html; UTF-8");  
    	       // NameValuePair[] param = { new NameValuePair("age", "11"),  
    	        //        new NameValuePair("name", "jay"), };  
    	        //method1.setRequestBody(param);  
    	    int statusCode=0;
    		try {
    			statusCode = client1.executeMethod(method1);
    		} catch (HttpException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}  
    	    System.out.println(statusCode);  
    	    try {
    			System.out.println(method1.getResponseBodyAsString());
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	    method1.releaseConnection();  
    	    System.out.println("...............post Done...............");
    		return new ModelAndView("success");
    		
    	}
    }
    

      发送get请求:

    @Controller
    public class AuthorizeCode {
        @RequestMapping(value="code.html")
        public ModelAndView requestPost(HttpServletRequest request){
            HttpClient client = new HttpClient();   
            GetMethod method = new GetMethod("http://localhost:8080/AuthServer/oauth2/authorize?client_id=4o7ymfOJbTNDSPPYvPqyFHvT&client_secret=jgjFw0l4e7oUUH09uH6oLUrW&redirect_uri=https://www.hao123.com&response_type=code&scope=10216051013292419216811211005:close");  
            
            //method.getParams().setParameter("client_id", "4o7ymfOJbTNDSPPYvPqyFHvT");
            //method.getParams().setParameter("client_secret", "jgjFw0l4e7oUUH09uH6oLUrW");
            //method.getParams().setParameter("redirect_uri", "http://localhost:8080/App/request.html");
           // method.getParams().setParameter("response_type", "code");
            //method.getParams().setParameter("scope", "10216051013292419216811211005:close");
            try {  
                // Execute the method.  
                int statusCode = client.executeMethod(method);  
                System.out.println(statusCode);  
                if (statusCode == HttpStatus.SC_OK) {  
                    //ins = method.getResponseBodyAsStream();  
                    String ss = method.getResponseBodyAsString();
                    System.out.println(ss);
                  
               } else {  
                    System.err.println("Response Code: " + statusCode);  
                }  
            } catch (HttpException e) {  
                System.err.println("Fatal protocol violation: " + e.getMessage());  
            } catch (IOException e) {  
                System.err.println("Fatal transport error: " + e.getMessage());  
            } finally {  
                method.releaseConnection();  
               // if (ins != null) {  
               //     ins.close();  
               // }  
            }  
            
            System.out.println("...............get Done...............");
            return new ModelAndView("code");
        }

    在项目中使用这两个方法需要引入jar包:

    commons-codec-1.6.jar;

    commons-httpclient-3.1-osgi.jar;

    commons-logging-1.1.1.jar;

    httpcore-4.0.jar。

  • 相关阅读:
    15. DML, DDL, LOGON 触发器
    5. 跟踪标记 (Trace Flag) 834, 845 对内存页行为的影响
    4. 跟踪标记 (Trace Flag) 610 对索引组织表(IOT)最小化日志
    14. 类似正则表达式的字符处理问题
    01. SELECT显示和PRINT打印超长的字符
    3. 跟踪标记 (Trace Flag) 1204, 1222 抓取死锁信息
    2. 跟踪标记 (Trace Flag) 3604, 3605 输出DBCC命令结果
    1. 跟踪标记 (Trace Flag) 1117, 1118 文件增长及空间分配方式
    0. 跟踪标记 (Trace Flag) 简介
    SpringBoot + Redis + Shiro 实现权限管理(转)
  • 原文地址:https://www.cnblogs.com/godlei/p/5560786.html
Copyright © 2011-2022 走看看