• 记录一次URL中有特殊字符怎么处理?


    你out了,赶紧换

    RestTemplate 吧!

    进入正题,直接实战!!!

    import java.util.HashMap;
    import java.util.Map;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.util.LinkedMultiValueMap;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.client.RestTemplate;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class TestUrl {
    
    	@Autowired
    	private RestTemplate restTemplate;
    
    	/*
    	 * get无参
    	 */
    	@Test
    	public void testNoParameter() {
    		String object = restTemplate.getForObject(
    				"http://127.0.0.1/findAllStorageDevice", String.class);
    		System.out.println("11111111111" + object);
    	}
    
    	/*
    	 * get有参
    	 */
    	@Test
    	public void testYesParameter() {
    		Map<String, String> map = new HashMap();
    		map.put("sdId", "res$cc$20180524113123$962c4ded-d1df-49ca-92d3-cfbca5eb28ea");
    		String object = restTemplate.getForObject(
    				"http://127.0.0.1/findStorageDeviceById?sdId={sdId}",
    				String.class, map);
    		System.out.println("11111111111" + object);
    	}
    
    	/*
    	 * post
    	 */
    
    	@Test
    	public void testPost() {
    		MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    		map.add("username", "11");
    		map.add("password", "22");
    		HttpHeaders headers = new HttpHeaders();
    		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
    		ResponseEntity<String> postForEntity = restTemplate.postForEntity("http://127.0.0.1/login", map,
    				String.class);
    		System.out.println("11111111111" + postForEntity);
    
    	}
    }
    

      

    package app.util;
    
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.ResponseEntity;
    import org.springframework.util.LinkedMultiValueMap;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.client.RestTemplate;
    public class urlTest {
    
    	
    	
    	public static void main(String[] args) throws InterruptedException {
    		
    			RestTemplate restTemplate = new RestTemplate();
    			MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
    			//map.add("opUsername", "32322");
    			HttpHeaders headers = new HttpHeaders();
    			restTemplate.setErrorHandler(new CustomErrorHandler());
    
    			headers.add("token", "5a140050-ef25-4fe2-ada7-7ae98d6d2246");
    
    			HttpEntity<MultiValueMap<String, Object>> httpEntity1 = new  HttpEntity<MultiValueMap<String, Object>>(map,headers);
    			String url1="http://127.0.0.1/find";
    			ResponseEntity<String> entity1 = restTemplate.exchange(url1, HttpMethod.GET, httpEntity1, String.class);
    			System.out.println("2"+entity1.getStatusCode());
    			System.out.println("2"+entity1.getBody());
    		
    	}
    	
    
    }
    

      

  • 相关阅读:
    tinymce原装插件源码分析(二)-link
    tinymce原装插件源码分析(一)-hr
    pyinstall 常见错误
    matlab Time-domain analysis 渐进式或者实时获取仿真值
    初识python和pycharm
    自定义指令详解
    Vue核心知识一览
    多维数组 转化为 一维数组
    js面试之数组的几个不low操作
    js如何操作或是更改sass里的变量
  • 原文地址:https://www.cnblogs.com/-flq/p/11498122.html
Copyright © 2020-2023  润新知