java鬼混笔记:http get post 请求
2017-06-29 23:41
441 查看
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.util.URIUtil; public class TestHttp { public static void main(String[] args) throws Exception { get(); } public static void get() { String httpUrl = "http://api.map.baidu.com/telematics/v3/weather?"; HttpURLConnection conn = null; BufferedReader reader = null; try { String params = "location=" + URLEncoder.encode("北京", "utf-8") + "&output=json&ak=57ab4d7f7882e2028de5a9a589ae697f"; URL url = new URL(httpUrl + params); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); // 设置头文件 // conn.setRequestProperty("xxx", "xxxx"); reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String line = null; StringBuffer sbf = new StringBuffer(); while ((line = reader.readLine()) != null) { sbf.append(line); } System.out.println(sbf.toString()); } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } if (conn != null) { conn.disconnect(); ; } } catch (Exception e) { e.printStackTrace(); } } } public static void post() { String httpUrl = "http://api.map.baidu.com/telematics/v3/weather?"; HttpURLConnection conn = null; BufferedReader reader = null; try { String params = "location=" + URLEncoder.encode("北京", "utf-8") + "&output=json&ak=57ab4d7f7882e2028de5a9a589ae697f"; URL url = new URL(httpUrl); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); // 设置头文件 // conn.setRequestProperty("xxx", "xxxx"); PrintWriter out = new PrintWriter(conn.getOutputStream()); out.write(params); out.flush(); out.close(); reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String line = null; StringBuffer sbf = new StringBuffer(); while ((line = reader.readLine()) != null) { sbf.append(line); } System.out.println(sbf.toString()); } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } if (conn != null) { conn.disconnect(); ; } } catch (Exception e) { e.printStackTrace(); } } } }
相关文章推荐
- Java学习笔记———《http的get请求和post请求及封装的工具类》
- java发送http的get、post请求
- Java模拟HTTP的Get和Post请求(增强) Apache-httpclient
- [Java] 模拟HTTP的Get和Post请求
- java HTTP get post请求,获取二进制文件实现
- java 用面向对象的方式发送http的GET和POST请求
- java发送post和get请求(1)---HttpClient 方式
- java http 请求get 和post
- Java发送HTTP的POST和GET方式请求
- java HttpClient get post请求 调用接口
- java发送http的get、post请求
- Java模拟HTTP的Get和Post请求
- java 发送http请求post get
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理(转)
- java发送post和get请求(1)---HttpURLConnection方式
- java 发送HTTP (get 和 post方法请求)
- 纯java版本的http get和post请求
- java发送http的get、post请求
- java HttpUrlConnection get和post 模拟请求 调用接口