1.Java使用HTTP的get方法读取网络数据
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();
//将字节流向字符流的转换
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
/**
* StringBuilder和StringBuffer不同的地方在于 StringBuffer是线程安全的
* 单线程、不需要线程安全的情况下,处于性能的考虑,优先选择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方法与网络交互通信
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");
//设置为true,connection就可以从网络获取数据
connection.setDoInput(true);
//设置为true,connection就可以向网络传送数据
connection.setDoOutput(true);
//把访问方式换成post方式
/**
* 使用方法时候要先发送数据,所以OutputStream放在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,然后判断line是否为空。
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();
}
}
}