前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >httpClient写简单的get请求访问百度网址和Springmvc本地controller

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

作者头像
全栈程序员站长
发布2022-06-30 13:22:43
4680
发布2022-06-30 13:22:43
举报
文章被收录于专栏:全栈程序员必看

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档