java java.net.url_java通过java.net.URL发送http请求调用接口

importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.HttpURLConnection;importjava.net.URL;/*** http工具

**/

public final classHttpUtil {/*** 模拟http协议发送get请求

**/

public staticString sendGetRequest(String url) {

String result= "";

InputStream in= null;

HttpURLConnection connection= null;try{

URL httpUrl= newURL(url);

connection=(HttpURLConnection) httpUrl.openConnection();

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("Charset", "UTF-8");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

connection.setRequestMethod("GET");

connection.connect();if (connection.getResponseCode() == 200) {

in=connection.getInputStream();

ByteArrayOutputStream bs= newByteArrayOutputStream();int n = 0;byte[] datas = new byte[2048];while ((n = in.read(datas)) != -1) {

bs.write(datas,0, n);

}

bs.flush();

result= new String(bs.toByteArray(), "utf-8");

}

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{if (in != null) {

in.close();

}

}catch(Exception e) {

e.printStackTrace();

}try{

connection.disconnect();

}catch(Exception ex) {

}

}returnresult;

}/*** 模拟http协议发送post请求

**/

public staticString sendPostRequest(String url, String param) {

InputStream in= null;

String result= "";

HttpURLConnection connection= null;try{

URL httpUrl= newURL(url);

connection=(HttpURLConnection) httpUrl.openConnection();

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("Charset", "UTF-8");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

connection.setRequestMethod("POST");

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setUseCaches(false);

connection.getOutputStream().write(param.getBytes("utf-8"));

connection.getOutputStream().flush();if (connection.getResponseCode() == 200) {

in=connection.getInputStream();

ByteArrayOutputStream bs= newByteArrayOutputStream();int n = 0;byte[] datas = new byte[2048];while ((n = in.read(datas)) != -1) {

bs.write(datas,0, n);

}

bs.flush();

result= new String(bs.toByteArray(), "utf-8");

}

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{if (in != null) {

in.close();

}

}catch(IOException ex) {

ex.printStackTrace();

}try{

connection.disconnect();

}catch(Exception ex) {

}

}returnresult;

}/*** 普通post文件上传*/

public staticString upLoadAttachment(String url, String fileName, File file) {

String result= null;

HttpURLConnection connection= null;

BufferedReader reader= null;try{

URL httpUrl= newURL(url);

connection=(HttpURLConnection) httpUrl.openConnection();

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("Charset", "UTF-8");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

connection.setRequestMethod("POST");

connection.setConnectTimeout(5000);

connection.setReadTimeout(5000);

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setUseCaches(false);

String bound= "----------" +System.currentTimeMillis();

connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" +bound);

StringBuilder sb= newStringBuilder();

sb.append("--");

sb.append(bound);

sb.append("\r\n");

sb.append("Content-Disposition: form-data;name=\"" + fileName +

"\";filename=\"" + file.getName() + "\"\r\n");

sb.append("Content-Type:application/octet-stream\r\n\r\n");byte[] data = sb.toString().getBytes("utf-8");

OutputStream out= newDataOutputStream(connection.getOutputStream());

out.write(data);

DataInputStream in= new DataInputStream(newFileInputStream(file));int bytes = 0;byte[] bufferOut = new byte[1024];while ((bytes = in.read(bufferOut)) != -1) {

out.write(bufferOut,0, bytes);

}

in.close();byte[] foot = ("\r\n--" + bound + "--\r\n").getBytes("utf-8"); //定义最后数据分隔线

out.write(foot);

out.flush();

out.close();

InputStream inn=connection.getInputStream();

ByteArrayOutputStream bs= newByteArrayOutputStream();int n = 0;byte[] datas = new byte[2048];while ((n = inn.read(datas)) != -1) {

bs.write(datas,0, n);

}

bs.flush();

result= new String(bs.toByteArray(), "utf-8");returnresult;

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{

connection.disconnect();

}catch(Exception ex) {

}

}return null;

}/*** post文件上传 - 使用InputStream输入流(例如MultipartFile从前端获取文件后转发给其他的服务器)*/

public staticString upLoadAttachment(String url,String keyName, String fileName, InputStream fins) {

String result= null;

HttpURLConnection connection= null;

BufferedReader reader= null;try{

URL httpUrl= newURL(url);

connection=(HttpURLConnection) httpUrl.openConnection();

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("Charset", "UTF-8");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

connection.setRequestMethod("POST");

connection.setConnectTimeout(5000);

connection.setReadTimeout(5000);

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setUseCaches(false);

String bound= "----------" +System.currentTimeMillis();

connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" +bound);

StringBuilder sb= newStringBuilder();

sb.append("--");

sb.append(bound);

sb.append("\r\n");

sb.append("Content-Disposition: form-data;name=\"" + keyName +

"\";filename=\"" + fileName + "\"\r\n");

sb.append("Content-Type:application/octet-stream\r\n\r\n");byte[] data = sb.toString().getBytes("utf-8");

OutputStream out= newDataOutputStream(connection.getOutputStream());

out.write(data);

DataInputStream in= newDataInputStream(fins);int bytes = 0;byte[] bufferOut = new byte[1024];while ((bytes = in.read(bufferOut)) != -1) {

out.write(bufferOut,0, bytes);

}

in.close();byte[] foot = ("\r\n--" + bound + "--\r\n").getBytes("utf-8"); //定义最后数据分隔线

out.write(foot);

out.flush();

out.close();

InputStream inn=connection.getInputStream();

ByteArrayOutputStream bs= newByteArrayOutputStream();int n = 0;byte[] datas = new byte[2048];while ((n = inn.read(datas)) != -1) {

bs.write(datas,0, n);

}

bs.flush();

result= new String(bs.toByteArray(), "utf-8");returnresult;

}catch(Exception ex) {

ex.printStackTrace();

}finally{try{

connection.disconnect();

}catch(Exception ex) {

}

}return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值