java post 库,JAVA java.net库发送get/post请求基础

1.get

packagejiekou_test_test2;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;import java.net.*;public classjuhewether_get {public static voidmain(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= newURL(url);

HttpURLConnection openConnection=(HttpURLConnection)juhe_wether_get_url.openConnection();

openConnection.connect();

InputStream is=openConnection.getInputStream();

InputStreamReader ir= new InputStreamReader(is, "utf-8");

BufferedReader br= newBufferedReader(ir);

StringBuffer sb= newStringBuffer();

String temp= "";while((temp = br.readLine()) != null) {

sb.append(temp);

}

String result=sb.toString();

System.out.print(result);

}catch(Exception e){

}finally{

}

}

}

2.POST

packagejiekou_test_test2;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;import java.net.*;public classjuhewether_post {public static voidmain(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= newURL(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= newBufferedReader(isr);//strore result

StringBuffer sb = newStringBuffer();

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值