package com.albertyy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/*
* 微信公众号:AlbertYang
*/
public class SendHttp {
public static void main(String[] args) {
String s1=sendGet("https://www.baidu.com","");
if(s1!=null) {
System.out.println(s1);
}
}
//发送get请求
public static String sendGet(String url, String param) {
StringBuffer result = new StringBuffer();
String urlName = url + "?" + param;
BufferedReader in = null;
try {
URL realURL = new URL(urlName);
HttpURLConnection conn = (HttpURLConnection) realURL.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "close");
conn.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
conn.connect();
// Map<String, List<String>> map = conn.getHeaderFields();
// for (String s : map.keySet()) {
// System.out.println(s + "-->" + map.get(s));
// }
if (conn.getResponseCode() == 200) {
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
while ((line = in.readLine()) != null) {
result.append(line);
}
}else {
in = new BufferedReader(
new InputStreamReader(conn.getErrorStream(), "utf-8"));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result.toString();
}
//发送请求
public static String sendPost(String url, String param) {
StringBuffer result = new StringBuffer();
PrintWriter out = null;
BufferedReader in = null;
try {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "close");
conn.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
if (conn.getResponseCode() == 200) {
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
while ((line = in.readLine()) != null) {
result.append(line);
}
}else {
in = new BufferedReader(
new InputStreamReader(conn.getErrorStream(), "utf-8"));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (out != null) {
out.close();
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result.toString();
}
}
Java使用HttpURLConnection模拟浏览器发送HTTP请求
原创wx5efde04e640b7 ©著作权
文章标签 Java发送HTTP请求 Java发送get Post请求 java chrome 文章分类 代码人生
下一篇:css实现彩色渐变滑动条
-
Android http通信 HttpURLConnection
post 请求:
android java ide -
Http学习之使用HttpURLConnection…
post请求的OutputStream实际上不是网络流,而是写入内存,在g
System 服务器 post请求 -
HttpURLConnection模拟浏览器+网络数据抓取
/** *网页抓取方法 * @param urlString 要抓取的url地址 * @param charset  
浏览器 nokia string null exception -
Http学习之使用HttpURLConnection发送post和get请求
最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在U
android java servlet url System -
通过HttpURLConnection模拟post表单提交
通过HttpURLConnection模拟post表单提交
java st表 连接超时 .net -
mysql假字段怎么当条件
以下是以Windows系统下实践为例:一、造假数据,模拟场景1.制造数据的存储过程:DELIMITER //CREATE PROCEDURE p1(num int)BEGINDECLARE v INT;SET v = 1;WHILE v <= num DOINSE
mysql假字段怎么当条件 mysql 优化策略 慢查询 sql语句 执行时间