URL的编码问题URLDecoder和URLEncoder
来源:互联网 发布:淘宝潘多拉代购真的吗 编辑:程序博客网 时间:2024/05/20 17:27
场景:
通过URL读取网页内容,但URL中有中文参数。
解决:
需要用URLEncoder对中文参进行转化
如:广州通过转码>>>URLEncoder.encode(“广州”,"utf-8")
得到:%E5%B9%BF%E5%B7%9E
案例:天气查询~
package com.temp.demo.util;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import com.temp.demo.bean.Result;/** * 查询天气工具类 * @author sunmnet-developer * */public class ReadUtil {public static Result findByCity(String cityName) throws Exception {//必须转码为encode类型String encode=URLEncoder.encode(cityName, "utf-8");//查询接口(免费)URL url=new URL("https://api.seniverse.com/v3/weather/now.json?key=8xpdyjgfcizdqyuc&location="+encode);HttpURLConnection conn=(HttpURLConnection) url.openConnection();//通过流读取内容BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));StringBuffer sb=new StringBuffer();String line="";while ((line=br.readLine())!=null) {sb.append(line);}System.out.println(sb);return null;}public static void main(String[] args) throws Exception {ReadUtil.findByCity("广州");}}
备注:
在群里看到有人问这个编码的问题,然后注意到他的事天气服务。。。就跟着瞎搞了,刚开始搞,编码问题就卡了一个下午,暂时没有后续...
阅读全文
0 0
- URL的编码问题URLDecoder和URLEncoder
- URL编码 百分号编码和解码 URLEncoder和URLDecoder的大致实现原理
- URL编码 百分号编码和解码 URLEncoder和URLDecoder的大致实现原理
- URL编码 百分号编码和解码 URLEncoder和URLDecoder的大致实现原理
- URL编码 百分号编码和解码 URLEncoder和URLDecoder的大致实现原理
- URL编码 百分号编码和解码 URLEncoder和URLDecoder的大致实现原理
- 编码 URLEncoder 和 解码 URLDecoder
- 编码 URLEncoder 和 解码 URLDecoder
- URLEncoder和URLDecoder类(解决url传递参数的问题)
- good -- URLEncoder.encode 和 URLDecoder.decode 处理url的特点
- URLDecoder解码、URLEncoder编码
- 使用URLEncoder、URLDecoder进行URL参数的转码与解码 encodeURIComponent编码
- URLDecoder和URLEncoder的使用总结
- java中的URLEncoder和URLDecoder类处理编码与解码问题
- URLEncoder 和 URLDecoder
- URLEncoder和URLDecoder
- URLDecoder 和URLEncoder类
- 使用URLDecoder和URLEncoder
- redis搜索hasp的值
- window服务器下如果建立两个目录之间的映射关系
- 解决找不到/usr/bin/ld: cannot find -lXX的问题
- 1!+2!+3!+、、、+n!的值
- 下载和安装源码包详解
- URL的编码问题URLDecoder和URLEncoder
- iOS通过dSYM文件分析crash
- Effect框架之一
- 摩尔定律还有效吗?
- 4用于cifar10的卷积神经网络-4.2/4.3cifar10卷积神经网络的计算图设计(上下)
- Spring Boot事务配置
- 提高MySQL性能的7个技巧
- vue.js之slot
- 第一章:Eclipse用Maven搭建SSM框架并部署到Tomcat7