httpClient写简单的get请求访问百度网址和Springmvc本地controller

2022-06-30 13:22:43 浏览数 (1)

1,get请求访问百度网址

代码语言:javascript复制
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;


public class TestGet {

	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		//1,创建一个httpClient对象
		CloseableHttpClient client=HttpClients.createDefault();
		//2,创建uriBuilder 对于httpClient4.3访问指定页面url必须要使用http://开始
		URIBuilder uriBuilder=new URIBuilder("http://www.baidu.com")
		//4,创建httpget对象
		HttpGet httpGet=new HttpGet(uriBuilder.build());
		//5,设置请求报文头部的编码
		httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; utf-8"));
		//6,设置期望服务返回的编码
		httpGet.setHeader(new BasicHeader("Accept","text/plain;charset=utf-8"));
		//7,请求服务
		CloseableHttpResponse response=client.execute(httpGet);
		//8,获取请求返回码
		int statusCode=response.getStatusLine().getStatusCode();
		//9如果请求返回码是200,则说明请求成功
		if(statusCode==200){
			//10,获取返回实体
			HttpEntity entity=response.getEntity();
			//11,通过EntityUtils的一个工具类获取返回的内容
			String str=EntityUtils.toString(entity);
			System.out.println("请求成功的返回内容:" str);
			
		}else{
			System.out.println("请求失败!");
		}
		response.close();
		client.close();
	}

}

2,访问springmvc本地启动的controller

main方法调用,本地启动的springMVC2工程,因此调用之前要先启动url的工程

代码语言:javascript复制
import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpResponse;  
import org.apache.http.client.ClientProtocolException;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.impl.client.DefaultHttpClient;  

import com.alibaba.fastjson.JSONObject;
import com.sun.jdi.Method;
  
  
public class HTTPGetSample {  
    public static void main(String[] args) throws Exception {  
    	String url = "http://localhost:8080/springMVC2/view?aa=ddddddddddddddddddddd";  
        
		DefaultHttpClient client = new DefaultHttpClient();  
        HttpGet request = new HttpGet(url);  
          
        HttpResponse response = client.execute(request);  
        System.out.println("Response Code: "    
        response.getStatusLine().getStatusCode());  
          
        BufferedReader rd = new BufferedReader(  
            new InputStreamReader(response.getEntity().getContent()));  
        String line = "";  
        while((line = rd.readLine()) != null) {  
        	System.out.println(line);  
        }
    } }

springmvc2的controller类:

代码语言:javascript复制
package com.springmvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ViewController {

    @RequestMapping("/view")
    public ModelAndView view(HttpServletRequest request,String aa){
        ModelAndView mav = new ModelAndView();
        String contextPath=request.getContextPath();
        mav.addObject("context",contextPath);
        mav.setViewName("index");
        System.out.println("SSSSSSSSSSSSSSSSSSSss" aa);
        return mav;
    }
}

如果执行main方法时在springmvc2工程的控制台会打印出aa所代表的参数,说明调用成功,并传承成功。

注:springMVC2的工程下载地址是:http://download.csdn.net/download/csdnliuxin123524/10001431

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106280.html原文链接:https://javaforall.cn

0 人点赞