测试HttpClient


import java.net.URLEncoder;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class TestHttpClient{

        public static void main(String[] args) {
           try {

        String word = URLEncoder.encode("电脑","UTF-8");
        String urlString ="http://fanyi.youdao.com/openapi.do?keyfrom=duandoudou&key=131209794&type=data&doctype=json&version=1.1&q="+word;

        HttpClient client = new HttpClient();

        GetMethod get = new GetMethod(urlString);

        client.executeMethod(get);

        int statusCode = get.getStatusCode();

        System.out.println("statusCode:"+statusCode);

        String result = new String(get.getResponseBodyAsString().getBytes("utf-8"));

        System.out.println("=======================result====================");
        System.out.println(result);

        get.releaseConnection();


    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

你可能感兴趣的:(httpclient,有道API)