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的更多相关文章

  1. java调用url接口

    很多简单的接口就是直接一个URl的形式, 怎么调用? HttpClient httpclient=null; PostMethod post=null; try{ httpclient = new H ...

  2. 调用URL 接口服务

    1.Net调用URL 接口服务 using System; using System.Collections; using System.Configuration; using System.Dat ...

  3. 【转】java通用URL接口地址调用方式GET和POST方式

    java通用URL接口地址调用方式GET和POST方式,包括建立请求和设置请求头部信息等等......... import java.io.ByteArrayOutputStream; import ...

  4. 使用Java的URL/HttpURLConnection进行远程调用(POST请求)

    利用Java的HttpURLConnection进行远程url请求(调用远程接口) 测试类:请求类型为json,以post方式请求,利用OutputStream写入数据 实体类: public cla ...

  5. java通过java.net.URL发送http请求调用接口

    一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...

  6. java通过url调用远程接口返回json数据

    java通过url调用远程接口返回json数据,有用户名和密码验证, 转自 https://blog.csdn.net/wanglong1990421/article/details/78815856 ...

  7. java后台调用url无协议

    url格式不正确,可能有"www.baidu.com"    "这个不能有 // 下载pdf public void downpdf(String URL, String ...

  8. java后台调用url

    版权声明:本文为博主牟云飞原创文章,未经博主同意不得转载. https://blog.csdn.net/myfmyfmyfmyf/article/details/32690757 QXOutStrea ...

  9. 【转】java调用webservice

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...

随机推荐

  1. 3、iptables扩展及使用

    iptables/netfilter netfilter: kernel framework,位于内核中的协议框架 iptables  是规则管理命令行工具 四表:filter, nat, mangl ...

  2. 动态 hover 使用变相使用

    使用   onmouseover  和 onmouseout 代替 hover foreach (var menu in Model.OrderBy(x => x.Order).Where(x ...

  3. Spring-json依赖

    <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jacks ...

  4. Shell中的IFS

    一.IFS 介绍 Shell 脚本中有个变量叫 IFS(Internal Field Seprator) ,内部域分隔符.完整定义是The shell uses the value stored in ...

  5. bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 16294  Solved: 6645[Submit ...

  6. oracle的批量操作sql语句

    1.批量删除/批量更新 mapper: <update id="updatePrjStateByFPrjId" parameterType="string" ...

  7. java四种访问权限符

    (PS:其中private和protected不能修饰一般的类,否则编译就会报“modifier private not allowed here”,如果是内部类就另当别论了)

  8. 合并k个排序的列表 Merge k Sorted Lists

    2018-11-25 22:58:52 问题描述: 问题求解: 本题可以使用优先队列高效的进行求解,整体的时间复杂度为O(nlogk). public ListNode mergeKLists(Lis ...

  9. Java将byte[]和int的互相转换

    /** * 将整数转换为byte数组并指定长度 * @param a 整数 * @param length 指定长度 * @return */ public static byte[] intToBy ...

  10. Superclass和Constructor Chaining

    A subclass inherits accessible date fields and methods from its superclass. Does it inherit construc ...