JAVA requests库发送get/post请求基础
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JAVA requests库发送get/post请求基础,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3245字,纯文字阅读大概需要5分钟。
内容图文
1.get
package jiekou_test_test2; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.*; public class juhewether_get { public static void main(String[] args) { // TODO Auto-generated method stub // set fillder proxy System.setProperty("http.proxySet", "true"); System.setProperty("http.proxyHost", "127.0.0.1"); System.setProperty("http.proxyPort", "8888"); String lon = "116"; String lat = "39"; String key = "bf74bdf2807069fd35a82e3aa19445d5"; String url = "http://v.juhe.cn/weather/geo"+"?"+"lon="+lon+"&"+"lat="+lat+"&"+"key="+key; try { URL juhe_wether_get_url = new URL(url); HttpURLConnection openConnection = (HttpURLConnection)juhe_wether_get_url.openConnection(); openConnection.connect(); InputStream is = openConnection.getInputStream(); InputStreamReader ir = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(ir); StringBuffer sb = new StringBuffer(); String temp = ""; while((temp = br.readLine()) != null) { sb.append(temp); } String result = sb.toString(); System.out.print(result); }catch(Exception e){ }finally { } } }
2.POST
package jiekou_test_test2; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.*; public class juhewether_post { public static void main(String[] args) { // TODO Auto-generated method stub //set fiddler proxy System.setProperty("http.proxySet", "true"); System.setProperty("http.proxyHost", "127.0.0.1"); System.setProperty("http.proxyPort", "8888"); String lon = "116"; String lat = "39"; String key = "bf74bdf2807069fd35a82e3aa19445d5"; String url = "http://v.juhe.cn/weather/geo"; //create a url URL post_url = null; HttpURLConnection openConnection = null; try { post_url = new URL(url); // open collection openConnection = (HttpURLConnection)post_url.openConnection(); // set request method openConnection.setRequestMethod("POST"); // set headers openConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // preapare data String data = "lon="+lon+"&"+"lat="+lat+"&"+"key="+key; // open write os openConnection.setDoOutput(true); //write data OutputStream os = openConnection.getOutputStream(); os.write(data.getBytes()); if(openConnection.getResponseCode() == 200) { // read inputstream InputStream is = openConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader br = new BufferedReader(isr); //strore result StringBuffer sb = new StringBuffer(); String temp = ""; while((temp=br.readLine()) != null) { sb.append(temp); } String result = sb.toString(); System.out.print(result); }else { System.out.print("status code is not 200!"); } } }catch(Exception e) { e.printStackTrace(); System.out.print("exception occurs"); }finally{ if(openConnection != null) { openConnection.disconnect(); } } }
3.记忆点
1.需要知道request的面向对象的设计思想,记住需要使用的类:
reqeust包:
URL
HttpURLConnection
IO流
2.真正发送请求的时机是 httpURLConnection.getInputStream方法的调用(通过fiddler抓包证实)
3.httpURLConnection.connect()方法不需要显示地调用,它的作用是建立tcp连接,如果不显示的调用,从connection获取输入流的时候也会先建立连接。
它更多的作用是区别设置request和获取response的时机。
4.参考博文:https://www.cnblogs.com/skys-li/p/6121044.html
内容总结
以上是互联网集市为您收集整理的JAVA requests库发送get/post请求基础全部内容,希望文章能够帮你解决JAVA requests库发送get/post请求基础所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。