1.Java使用HTTP的get方法讀取網(wǎng)絡(luò)數(shù)據(jù)
package peixun;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class TestGet {
public static void main(String[] args) {
new ReadByGet().start();
}
static class ReadByGet extends Thread {
@Override
public void run() {
try {
URL url = new URL("http://fanyi.youdao.com/openapi.do?keyfrom=yyxiaozhan&key=1696230822&type=data&doctype=xml&version=1.1&q=welcome");
//使用方法打開鏈接,并使用connection接受返回值
URLConnection connection = url.openConnection();
//獲取connection輸入流,并用is接受返回值
InputStream is = connection.getInputStream();
//將字節(jié)流向字符流的轉(zhuǎn)換
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
/**
* StringBuilder和StringBuffer不同的地方在于 StringBuffer是線程安全的
* 單線程泊交、不需要線程安全的情況下欧瘪,處于性能的考慮沾凄,優(yōu)先選擇StringBuilder
*/
StringBuilder builder = new StringBuilder();
while ((line = br.readLine())!= null) {
builder.append(line);
}
br.close();
isr.close();
is.close();
System.out.println(builder.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.Java使用HTTP的post方法與網(wǎng)絡(luò)交互通信
package peixun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class TestPost {
public static void main(String[] args) {
new ReadByPost().start();
}
}
class ReadByPost extends Thread{
@Override
public void run() {
try {
URL url = new URL("http://fanyi.youdao.com/openapi.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("encoding", "UTF-8");
//設(shè)置為true焚廊,connection就可以從網(wǎng)絡(luò)獲取數(shù)據(jù)
connection.setDoInput(true);
//設(shè)置為true,connection就可以向網(wǎng)絡(luò)傳送數(shù)據(jù)
connection.setDoOutput(true);
//把訪問方式換成post方式
/**
* 使用方法時候要先發(fā)送數(shù)據(jù)撤防,所以O(shè)utputStream放在InputStream前面
*/
connection.setRequestMethod("POST");
//connection建立鏈接之后虽风,要獲取輸入流和輸出流
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("keyfrom=yyxiaozhan&key=1696230822&type=data&doctype=xml&version=1.1&q=welcome");
bw.flush();
//connection建立鏈接之后,要獲取輸入流和輸出流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//進行讀取操作
String line;
StringBuilder builder = new StringBuilder();
while ((line = br.readLine())!=null) {//br.readline先賦值給line寄月,然后判斷l(xiāng)ine是否為空辜膝。
builder.append(line);
}
bw.close();
osw.close();
os.close();
br.close();
isr.close();
is.close();
System.out.println(builder.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}