java执行http请求乱码解决
2015-06-12 15:53
477 查看
痛过java进行http请求会出现乱码解决方式:
URLEncoder.encode(String s, String enc) //参数
使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式
URLDecoder.decode(String s, String enc)
使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。
发送的时候使用URLEncoder.encode编码,接收的时候使用URLDecoder.decode解码,都按指定的编码格式进行编码、解码,可以保证不会出现乱码
String url = "http://localhost/users/update?user_id="+ id +"&user_name="+URLEncoder.encode(changeName, "UTF-8");
String result = httpURLConnection.post(url, headers, "");
URLEncoder.encode(String s, String enc) //参数
使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式
URLDecoder.decode(String s, String enc)
使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。
发送的时候使用URLEncoder.encode编码,接收的时候使用URLDecoder.decode解码,都按指定的编码格式进行编码、解码,可以保证不会出现乱码
String url = "http://localhost/users/update?user_id="+ id +"&user_name="+URLEncoder.encode(changeName, "UTF-8");
String result = httpURLConnection.post(url, headers, "");
public String post(String url, Map<String, String> headers, String postData) throws Exception { BufferedReader in = null; HttpURLConnection conn = null; try { StringBuffer result = new StringBuffer(); URL realUrl = new URL(url); conn = (HttpURLConnection) realUrl.openConnection(); if(headers != null) { for(Map.Entry<String, String> header : headers.entrySet()) { conn.setRequestProperty(header.getKey(),header.getValue()); } } // 设置通用的请求属性 conn.setRequestMethod("POST"); conn.setRequestProperty("Accept", "*/*"); //conn.setRequestProperty("Accept-Encoding", "gzip"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setDoOutput(true); conn.setDoInput(true); // Post 请求不能使用缓存 conn.setUseCaches(false); conn.setConnectTimeout(3000); conn.setReadTimeout(5000); if(!StringUtils.isBlank(postData)) { OutputStream out = conn.getOutputStream(); out.write(postData.getBytes("utf-8")); out.flush(); out.close(); } in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "utf-8")); String line = ""; while ((line = in.readLine()) != null) { result.append(line); } return result.toString(); }catch(Exception e) { throw e; }finally { if (in != null) { in.close(); } } }
@Override public String get(String url, Map<String, String> headers) throws Exception { HttpURLConnection conn = null; BufferedReader bufReader = null; InputStream in = null; try { StringBuilder sb = new StringBuilder(); conn = (HttpURLConnection) new URL(url).openConnection(); conn.setDoInput(true); conn.setDoOutput(true); if(headers != null) { for(Map.Entry<String, String> header : headers.entrySet()) { conn.setRequestProperty(header.getKey(),header.getValue()); } } //conn.setRequestProperty("Accept-Encoding", "gzip"); conn.setConnectTimeout(4000); conn.setReadTimeout(120000); in = conn.getInputStream(); // int status = conn.getResponseCode(); // if(conn.getRequestProperty("Accept-Encoding").contains("gzip")) { // if(in != null) // in = new GZIPInputStream(in); // } bufReader = new BufferedReader(new InputStreamReader(in, "utf-8")); String newsContents = ""; while ((newsContents = bufReader.readLine()) != null) { sb.append(newsContents); } return sb.toString(); }catch(Exception e) { throw e; }finally { if(bufReader != null) bufReader.close(); if(in != null) in.close(); } }
相关文章推荐
- udp tcp
- Android 中 HttpClient作客户端,Tomcat Servlet作服务器的交互示例
- 做一个程序来查询手机号码的归属地(使用的是HttpURLConnection的post提交方式)
- Http协议与TCP协议简单理解
- Linux centos 连接网络
- 【Floyd】BZOJ1491: [NOI2007]社交网络
- Fat-tree 胖树交换网络
- 我们常常会从网络获取数据,而数据通常又为JSON格式,JSON数据转为Model对象
- HTTP常见状态码
- linux 下网络流量监控
- 无线路由器桥接、中继、电力猫组网方法分享
- 网络编程基础:1
- Java课程实验报告 实验四 Java网络编程及安全
- 本地连接和无线网络连接控制
- OC—网络监控 使用Reachability
- Socket套接字通信原理
- HTTP请求头
- TCP/IP TIME_WAIT状态原理
- 在.NET4.5项目中添加HttpClient引用的办法
- Java 代码实现Http 的GET和POST 请求