使用spring-web中的api发送http或https请求的代码
2015-04-01 00:00
706 查看
摘要: 这个类里面的功能严重依赖于spring-web包,有部分代码用的commons-io包里面的api.包括常用的get,post请求,文件的上传及下载等.特此记录,以备参考.
ClientHttpRequestFactory此处的实现类用的是org.springframework.http.client.SimpleClientHttpRequestFactory.
MultipartEntityBuilder类用的是httpmime-4.3.3.jar里的api,这个在文件上传方法里用到.
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,这个在文件上传方法里用到.
相关文章推荐
- (微信API接口开发) 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- c#使用Socket发送HTTP/HTTPS请求的实现代码
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求 .
- 使用Java原生API编写发送HTTP_POST请求的工具类
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求【转载】
- 使用HttpWebRequest发送自定义POST请求
- 使用HttpWebRequest发送自定义POST请求
- 使用 WinInte API 发送 HTTP 请求
- 使用HttpWebRequest发送自定义POST请求
- 使用java代码发送HTTP Post请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- 使用HttpWebRequest发送自定义POST请求(以http方式推送消息)
- Android中使用Json和Xml与服务器进行通信,使用代码发送Get和Post请求,http请求辅助类
- 使用HttpWebRequest发送自定义POST请求
- 使用 WinInte API 发送 HTTP 请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- (转载)在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求