您的位置:首页 > 理论基础 > 计算机网络

使用spring-web中的api发送http或https请求的代码

2015-04-01 00:00 706 查看
摘要: 这个类里面的功能严重依赖于spring-web包,有部分代码用的commons-io包里面的api.包括常用的get,post请求,文件的上传及下载等.特此记录,以备参考.

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.FileCopyUtils;
/**
* 发送http和https请求的工具类
*
*/
public class HttpSender {
private ClientHttpRequestFactory factory = null;

public ClientHttpRequestFactory getClientHttpRequestFactory() {
return factory;
}

public void setClientHttpRequestFactory(ClientHttpRequestFactory factory) {
this.factory = factory;
}

public String doGet(String url) throws IOException{
/*使用spring-core包的api发送get请求
ClientHttpResponse response = null;
try {
ClientHttpRequest request = this.factory.createRequest(new URI(url), HttpMethod.GET);
response = request.execute();
String rs = new String(FileCopyUtils.copyToByteArray(response.getBody()));
return rs;
} catch (URISyntaxException e) {
throw new IllegalArgumentException("url parameter is error!");
} finally {
try{
response.close();
}catch(Exception e){}
}
//*/
//使用commons-io包的api发送get请求
return IOUtils.toString(new URL(url));
}

public String doPost(String url, MediaType contentType, String param) throws IOException{
ClientHttpResponse response = null;
try {
ClientHttpRequest request = this.factory.createRequest(new URI(url), HttpMethod.POST);
request.getHeaders().setContentType(contentType);
request.getBody().write(param.getBytes());
response = request.execute();
String rs = new String(FileCopyUtils.copyToByteArray(response.getBody()));
return rs;
} catch (URISyntaxException e) {
throw new IllegalArgumentException("url parameter is error!");
} finally {
try{
response.close();
}catch(Exception e){}
}
}

public String upload(String urlStr, String mediaUrl) throws IOException {
try {
URL url = new URL(mediaUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
InputStream in = con.getInputStream();
String contentType = con.getContentType();
String filename = System.currentTimeMillis() + "." + contentType.substring(contentType.indexOf("/") + 1);
return this.upload(urlStr, in, filename);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("url parameter is error!");
}
}

public String upload(String url, InputStream stream, String filename) throws IOException {
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(url), HttpMethod.POST);
request.getHeaders().setContentType(MediaType.MULTIPART_FORM_DATA);
MultipartEntityBuilder.create().addBinaryBody("media", stream, ContentType.DEFAULT_BINARY, filename).build().writeTo(request.getBody());;
response = request.execute();
byte[] bs = FileCopyUtils.copyToByteArray(response.getBody());
return new String(bs);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("url parameter is error!");
} finally {
try {
response.close();
stream.close();
} catch (Exception e) {
}
}
}

public File download(String url, String filepath) throws IOException {
try {
File file = new File(filepath);
FileUtils.copyURLToFile(new URL(url), file);
return file;
} catch (MalformedURLException e) {
throw new IllegalArgumentException("url parameter is error!");
}
}
}

ClientHttpRequestFactory此处的实现类用的是org.springframework.http.client.SimpleClientHttpRequestFactory.

MultipartEntityBuilder类用的是httpmime-4.3.3.jar里的api,这个在文件上传方法里用到.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  http request