YuFeng

java 网络请求参数
//通过网址创建一个URL对象 URL url = new URL("网址URL"); //打...
扫描右侧二维码阅读全文
25
2018/06

java 网络请求参数

//通过网址创建一个URL对象
URL url = new URL("网址URL");
//打开连接并且设置相应的头请求
HttpURLConnection open = (HttpURLConnection)url.openConnection();
//设置UA参数
 open.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
//设置编码格式
open.setRequestProperty("Charsert", "UTF-8");
//设置请求的方式为post
open.setRequestMethod("POST");
//设置加载网页时间和读取时间,如果超时会报错 IO流Exception
open.setConnectTimeout(60*1000);
open.setReadTimeout(60*1000);
//设置缓存参数.默认不缓存
open.setUseCaches(false);
//post请求必须实现如下两行
open.setDoInput(true);
open.setDoOutput(true);
//通过网络连接获取相应的输出流.如果输出的书库不包含汉子等相关信息,不用指定编码.
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(open.getOutputStream(),"UTF-8"));
bw.write("发送的数据信息");
bw.flush();  //刷新缓存层
bw.close();   //关闭输出流
BufferedReader br = new BufferedReader(new InputStreamReader(open.getInputStream(),"UTF-8"));
//读取返回的信息
String line = null;
While((line = br.readLine)){
System.out.println(line);
}
Last modification:June 25th, 2018 at 01:14 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment