java调用url
1
try {
String str;
URL u = new URL("https://www.baidu.com");
InputStream is = u.openStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
if (br.ready()) {
while ((str = br.readLine()) != null) {
System.out.println(str);
}
}
br.close();
isr.close();
is.close();
} catch (MalformedURLException e) {
// url地址错误
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map; public class HttpRequest {
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
} /**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}
public static void main(String[] args) {
//发送 GET 请求
String s=HttpRequest.sendGet("http://...com?p1=1&p2=2");
System.out.println(s); //发送 POST 请求
String sr=HttpRequest.sendPost("http://...com?p1=1&p2=2");
System.out.println(sr);
}
java调用url的更多相关文章
- java调用url接口
很多简单的接口就是直接一个URl的形式, 怎么调用? HttpClient httpclient=null; PostMethod post=null; try{ httpclient = new H ...
- 调用URL 接口服务
1.Net调用URL 接口服务 using System; using System.Collections; using System.Configuration; using System.Dat ...
- 【转】java通用URL接口地址调用方式GET和POST方式
java通用URL接口地址调用方式GET和POST方式,包括建立请求和设置请求头部信息等等......... import java.io.ByteArrayOutputStream; import ...
- 使用Java的URL/HttpURLConnection进行远程调用(POST请求)
利用Java的HttpURLConnection进行远程url请求(调用远程接口) 测试类:请求类型为json,以post方式请求,利用OutputStream写入数据 实体类: public cla ...
- java通过java.net.URL发送http请求调用接口
一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...
- java通过url调用远程接口返回json数据
java通过url调用远程接口返回json数据,有用户名和密码验证, 转自 https://blog.csdn.net/wanglong1990421/article/details/78815856 ...
- java后台调用url无协议
url格式不正确,可能有"www.baidu.com" "这个不能有 // 下载pdf public void downpdf(String URL, String ...
- java后台调用url
版权声明:本文为博主牟云飞原创文章,未经博主同意不得转载. https://blog.csdn.net/myfmyfmyfmyf/article/details/32690757 QXOutStrea ...
- 【转】java调用webservice
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...
随机推荐
- HDU 5782 Cycle(KMP+哈希)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5782 题意:给出两个长度相等的字符串,输出两个字符的每个前缀是否循环相同. 思路: 如果连个串循环相 ...
- Java—网络编程基础
URL的应用: 获得网页源代码可以用字节流.字符流,流的获取可以用url.openStream(),也可以用con.getInputStream(): 字节流: URL url = new URL(& ...
- cp命令覆盖文件时不用按Y来确认的方法
我们在Linux下使用cp命令复制文件时候,有时候会需要覆盖一些同名文件,覆盖文件的时候都会有提示:需要不停的按Y来确定执行覆盖.文件数量不多还好,但是要是几百个估计按Y都要吐血了,于是折腾来半天总结 ...
- 前端阶段_html部分
HTML 1.html5的第一行一定是<!DOCTYPE html>,h4太长,而且一般ide中会自动加载,了解即可. 2.h5的整个页面被<html></html> ...
- Calendar的使用注意
一.Calendar和GregorianCalendar的关系 GregorianCalendar的一点: // 初始化 Gregorian 日历 // 使用当前时间和日期 // 默认为本地时间和时区 ...
- 《剑指offer》第五十题(字符串中第一个只出现一次的字符)
// 面试题50(一):字符串中第一个只出现一次的字符 // 题目:在字符串中找出第一个只出现一次的字符.如输入"abaccdeff",则输出 // 'b'. #include & ...
- Debug记录(1)
今天下午在给nRF52832写程序时,莫名遇到了这个错误 错误id是一个很奇怪的数. 原代码如下: static void timers_init(void) { uint32_t timer_err ...
- springboot之启动方式
我们在ideal中启动springboot项目时时不需要加载Tomcat容器的,直接在启动类启动就行了,原因是springboot项目中默认包含了内置Tomcat //springboot项目必须引入 ...
- SSD固态硬盘是会掉速的。
也没什么好的办法. 只是自己不再疑神疑鬼,总觉得中病毒了. 下面的文章还是挺有参考意义的. http://diy.pconline.com.cn/627/6271636_all.html SSD变慢了 ...
- Java 反射(简单捋一下)
有Student类,Person类,还有一个叫Class的类,这是反射的源头. 正常方式:通过完整的类名 > 通过new实例化 > 取得实例化对象 反射方式:实例化对象 > getC ...