httpClient使用非常方便,远程请求url路径获取返回数据,数据类型可以是字符串,字节数组。httpClient顾名思义,实现了http协议的客户端。值得注意的是请求之后,一定要关闭请求。
import java.io.IOException;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Service;
import com.chinaedu.nstats.service.IServiceUtils;
@Service("serviceUtils")
public class ServiceUtils implements IServiceUtils {
public String execute(String url){
HttpClient httpClient = new HttpClient();
GetMethod getMethod = null;
getMethod = new GetMethod(url);
String responseBody=null;
try {
int statusCode = httpClient.executeMethod