网络编程D6章+D7章
HTTP 协议中,get 方法和 post 方法的区别

什么是 cookie?cookie 有什么作用?

使用 URLConnection 类编写程序的一般步骤。




掌握利用 URLConnection 从网络中获取网页数据。(考虑乱码)
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Demo3 {
public static void main(String[] args) {
try {
String encoding = "UTF-8";
URL u = new URL("https://www.baidu.com/");
URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int encodingStart = contentType.indexOf("charset=");
if(encodingStart != -1){
encoding = contentType.substring(encodingStart+8);
System.out.print(encoding);
}
InputStream raw = uc.getInputStream();
Reader reader = new InputStreamReader(raw, encoding);
BufferedReader br = new BufferedReader(reader);
FileWriter fos = new FileWriter(new File("D:\\upload\\index2.html"));
BufferedWriter bw = new BufferedWriter(fos);
String line;
while((line = br.readLine()) != null) {
bw.write(line);
}
bw.close();
fos.close();
reader.close();
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
URLConnection 类的 doInput 属性和 doOutput 属性含义及使用方法

URL 和 URLConnection 这两个类功能上的不同。
一个负责描述URL,一个负责连接到URL。
各种 getter 方法获取首部字段(理解)。

理解 getHeaderField()方法的使用

package test;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Demo4 {
public static void main(String[] args) {
try {
URL u = new URL("https://www.baidu.com/");
URLConnection uc = u.openConnection();
int i = 1;
String header = uc.getHeaderField(i);
while(header!=null){
System.out.println(uc.getHeaderFieldKey(i)+":"+header);
i++;
header = uc.getHeaderField(i);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
了解 HttpURLConnection 类的特点


浙公网安备 33010602011771号