Java中HTTP通信

  Java自带的get、post请求:

  get请求方式:

package com.java;

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;
/**
* java自带get请求方式
* @author yxm
*
*/
public class HttpGet {
public static void main(String[] args) {
new Get().start();
}
static class Get extends Thread{
@Override
public void run() {
try {
URL url = new URL("http://fanyi.youdao.com/openapi.do?keyfrom=huhailang&key=557910319&type=data&doctype=xml&version=1.1&q=welcome");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); String line;
StringBuffer builder = new StringBuffer();
while((line=br.readLine())!=null){
builder.append(line);
}
br.close();
isr.close();
is.close();
System.out.println(builder.toString()); } catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
}
}
}
}

  Java自带的post请求:

package com.java;
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;
import java.net.URLConnection;
/**
* Java自带的post请求
* @author yxm
*
*/
public class HttpPost { public static void main(String[] args) {
// TODO Auto-generated method stub
new Post().start();
}
static class Post extends Thread{
@Override
public void run() {
try {
//http://fanyi.youdao.com/openapi.do?keyfrom=huhailang&key=557910319&type=data&doctype=xml&version=1.1&q=welcome
URL url = new URL("http://fanyi.youdao.com/openapi.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("encoding", "UTF-8");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST"); OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("keyfrom=huhailang&key=557910319&type=data&doctype=xml&version=1.1&q=welcome");
bw.flush(); 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){
builder.append(line);
} bw.close();
osw.close();
os.close();
br.close();
isr.close();
is.close(); System.out.println(builder.toString()); } catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
}
}
} }

  Apache提供的HTTPclient,同样也包含get和post两种方式

  httpclient下载地址:http://hc.apache.org/downloads.cgi

  Java中HTTP通信

  get方式:

public class HttpGets {
public static void main(String[] args) {
new Get().start();
} static class Get extends Thread{
HttpClient client = HttpClients.createDefault();
@Override
public void run() {
HttpGet get = new HttpGet("www.baidu.com"); HttpResponse reponse = client.excute(get);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString("encoding","UTF-8"); System.out.println(result);
}
}
}

  post方式:

public class HttpPosts {

    public static void main(String[] args) {
new Post().start();
}
static class Post extends Thread{
HttpClient client = HttpClients.createDefault();
@Override
public void run() {
HttpPost post = new HttpPost("http://fanyi.youdao.com/openapi.do"); List<BasicNameValuePair> paramters = new ArrayList<>();
paramters.add(new BasicNameValuePair("keyform","huhailang"));
paramters.add(new BasicNameValuePair("key","557910319"));
paramters.add(new BasicNameValuePair("type","data"));
paramters.add(new BasicNameValuePair("doctype","xml"));
paramters.add(new BasicNameValuePair("version","1.1"));
paramters.add(new BasicNameValuePair("q","welcome")); post.setEntity(new UrlEncodedFormEntity(paramters,"UTF-8"));
HttpResponse reponse = client.excute(post);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString("encoding","UTF-8"); System.out.println(result);
}
} }
上一篇:Java中的DateFormatter


下一篇:Linux磁盘管理:LVM逻辑卷的创建及使用