JAVA訪問網頁
分別測試使用get和post方法訪問網頁,可以收到服務器的請求,並寫入到html文件中。
import java.io.*;
import java.net.*;
import java.util.*;
public class TestGetPostPage {
// param:請求參數,格式應該滿足name1=value1&name2=value2的形式。
public static String sendGet(String url, String param) {
String result = "";
if (param != null) {
url = url + "?" + param;
}
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
HttpURLConnection conn = getHttpURLConnection(realUrl);
// 打印頭信息
printHeader(conn);
// 獲取響應
result = getResponse(conn);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// param:請求參數,格式應該滿足name1=value1&name2=value2的形式。
public static String sendPost(String url, String param) {
String result = "";
try {
URL realUrl = new URL(url);
HttpURLConnection conn = getHttpURLConnection(realUrl);
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
{// Post發送參數:
// 獲取HttpURLConnection 對象對應的輸出流
PrintWriter out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
out.close();
}
// 獲取響應
result = getResponse(conn);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 提供主方法,測試發送GET請求和POST請求
public static void main(String args[]) {
String url = "http://www.shicimingju.com/book/xiyouji.html";
// "https://zhuanlan.zhihu.com/hulaoshi";
String param = null;
// 也可以自己寫個Servlet測試是否接收到參數
// url = "http://localhost:8080/TestJavaWeb/AHServlet";
// param = "uname=tiger";
// ----------------------------
String s;
// 發送GET請求
s = TestGetPostPage.sendGet(url, param);
write("http_get.html", s);
System.out.println("-----------------------------------------------");
// 發送POST請求
s = TestGetPostPage.sendPost(url, param);
write("http_post.html", s);
}
private static HttpURLConnection getHttpURLConnection(URL realUrl) {
StringBuilder sb = new StringBuilder();
sb.append("Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
sb.append(" AppleWrbKit/537.36(KHTML, like Gecko)");
sb.append(" Chrome/72.0.3626.119 Safari/537.36");
HttpURLConnection conn = null;
try {
// 打開和URL之間的連接
conn = (HttpURLConnection) realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
return conn;
}
private static String getResponse(HttpURLConnection conn) {
// 讀取URL的響應
String result = "";
try (InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader in = new BufferedReader(isr)) {
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (Exception e) {
System.out.println("Err:getResponse()");
e.printStackTrace();
} finally {
conn.disconnect();
}
System.out.println("getResponse():" + result.length());
return result;
}
private static void printHeader(HttpURLConnection conn) {
System.out.println("---↓↓↓響應頭字段---");
Map<String, List<String>> map = conn.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
System.out.println("---↑↑↑響應頭字段---");
}
private static void write(String fileName, String text) {
File f = new File(fileName);
try (FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);) {
bw.write(text);
} catch (IOException e) {
e.printStackTrace();
}
}
}
還可以自己寫個Servlet測試服務器端是否接收到參數:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/AHServlet")
public class AHServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String parameter = request.getParameter("uname");
System.out.println("Get : " + parameter);
resp(response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String parameter = request.getParameter("uname");
System.out.println("Post : " + parameter);
resp(response);
}
private void resp(HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("https://www.bilibili.com/video/av48272174/?p=2");
out.flush();
out.close();
}
}
*JAVA多線程下載網絡文件
[參見]: https://www.cnblogs.com/tigerlion/p/10661367.html
URLEncoder和URLDecoder
在使用百度搜索關鍵字的時候,往往會在地址欄看到如下內容:
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=%E8%99%8E%E8%80%81%E7%8B%AE
有的瀏覽器直接看不到這段內容,地址欄中直接顯示輸入的關鍵字。這是因為瀏覽器對其進行了修飾,避免神秘代碼對用戶造成困擾,一般IE不會做這種修飾。
這里的“%E8%99%8E%E8%80%81%E7%8B%AE”就是你輸入的關鍵字,但是進行了編碼。
編碼,可以使用URLDecoder.decode(...)方法
解碼,可以使用URLEncoder.encode(...)方法
encoder:編碼器。
decoder:解碼器。URL:Uniform Resource Locator,統一資源定位符。互聯網上的每個文件都有一個唯一的
URL。
普通字符串 ←→ application/x-www-form-rulencoded MIME字符串
效果在於轉換漢字等。
import java.net.*;
public class _12URLDecoderTest {
public static void main(String[] args) throws Exception {
// 將application/x-www-form-urlencoded字符串
// 轉換成普通字符串
String keyWord = URLDecoder.decode("%E8%99%8E%E8%80%81%E7%8B%AE", "UTF-8");
System.out.println(keyWord);
// 將普通字符串轉換成
// application/x-www-form-urlencoded字符串
String urlStr = URLEncoder.encode("無字真經", "GBK");
System.out.println(urlStr);
urlStr = URLEncoder.encode("無字真經", "UTF-8");
System.out.println(urlStr);
}
}
運行結果:
虎老獅
%CE%DE%D7%D6%D5%E6%BE%AD
%E6%97%A0%E5%AD%97%E7%9C%9F%E7%BB%8F