记录一次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()); } }
记录一次URL中有特殊字符怎么处理?的更多相关文章
- URLEncode解决url中有特殊字符的问题
问题:图片上传后的url地址中有&等特殊字符,页面传到后端时被自动处理成了& 解决:前端对url进行URLEncode,后端收到后进行URLDecode 总结:需要在请求u ...
- MVC 记录操作日志与过滤特殊字符
最近进行的MVC系统需要用到记录操作日志和过滤特殊字符的功能,如果每个action中都调用记录日志的方法就太麻烦了,所以根据需要结合mvc的过滤机制 写了个特殊字符验证与记录操作日志的公用类: pub ...
- curl 下载地址中有特殊字符解决方案
curl 下载地址中有特殊字符解决方案 情况 使用 curl 下载 地址中带有 特殊字符的时候 比如下面这个地址.实际访问地址不正确,参数丢失问题 curl -o kspf.jpeg https:// ...
- 网址URL中特殊字符转义编码
网址URL中特殊字符转义编码字符 - URL编码值空格 - %20" - %22# - %23% - %25& - %26( - %28) - %29+ - %2B, - %2C/ ...
- Tomcat8升级后URL中特殊字符报错出现原因
请求带上花括号等字符,请求无法送达服务端,报错: Failed to load resource: the server responded with a status of 400 () https ...
- URL传值特殊字符处理
问题:url传递的值中有特特殊字符,比如"<"或者">"会导致传递的参数被截短,也就是特殊字符之后的值传递不过去(例如:var list=" ...
- Mysql删除表名中有特殊字符的表
由于公司业务和应用的调整,之前在Mysql中的很多表都不需要了,故需要对数据库进行整理. 刚开始,我在想:不就删除一些表吗?很好解决,写个简单的脚本就可以了.我先看了数据库中有80000多个表,很 ...
- 033 Url中特殊字符的处理
在url跳转页面的时候,参数值中的#不见了,一直没有处理,今天有空看了一下,后来发现后台的过滤器之类的都没有处理,就比较奇怪了,原来是特殊字符的问题. 一:Url中的特殊字符 1.说明 这里还是需要做 ...
- js中有特殊字符的编码格式
在get和post方法中,如果传入的参数值有特殊字符,如:“&”,在get中的url需要拼接,可以使用encodeURICompontent来编码来转化 回调就是在上面传递实际参数,传递给aj ...
随机推荐
- Linux 安装gcc g++
Linux版本: cat /proc/version Linux version 3.10.0-693.11.6.el7.x86_64 (mockbuild@x86-041.build.eng.bos ...
- 超低功耗2.4G收发一体: SI24R1
Si24R1是一颗工作在2.4GHz ISM频段,专为低功耗无线场合设计,集成嵌入式ARQ基带协议引擎的无线收发器芯片.工作频率范围为2400MHz-2525MHz,共有126个1MHz带宽的信道.同 ...
- 找不到xml、找不到类
解决java.lang.ClassNotFoundException 解决 java.io.FileNotFoundException: class path resource [xxx.xml] ...
- Mysql 事务隔离级别分析
Mysql默认事务隔离级别是:REPEATABLE-READ --查询当前会话事务隔离级别mysql> select @@tx_isolation; +-----------------+ | ...
- java核心-JVM-gc面试题
1.写一个memory leak的例子 public class MemonyLeak { //1.memoryLeak内存泄漏 /* 这类错误报错具体显示:java.lang.OutOfMemory ...
- Apache使用mod_jk/mod_proxy模块实现代理、负载均衡的配置
转载自:https://blog.csdn.net/yuanyuan_186/article/details/51290518 192.168.1.123/192.168.1.121为后端tomcat ...
- 第二周的java
- 使用Oracle VM VirtualBox安装CentOS 7.6操作系统
使用Oracle VM VirtualBox安装CentOS 7.6操作系统 ...
- 【golang】golang的一些知识要点
特殊常量iota: 1.iota的值在遇到const关键字时将被重置为0 2.const中每新增一行常量声明将使iota计数一次,也就是自动加一. 3.iota只能在常量定义中使用. iota常见使用 ...
- 如何下载Github的工程到本地,完成修改并提交
2019-02-22 对以下方法做了测试 可以生效 1 在Github上添加密钥 方法是:https://www.cnblogs.com/jason-beijing/p/9110187.html 2 ...