在这篇文章中,我们将带领您了解HttpUtils的全貌,同时,我们还将为您介绍有关AndroidHttp相关辅助类HttpUtils、Android开源框架(三)基于OkHttp进一步封装的OkHtt
在这篇文章中,我们将带领您了解HttpUtils的全貌,同时,我们还将为您介绍有关Android Http相关辅助类 HttpUtils、Android 开源框架 (三) 基于 OkHttp 进一步封装的 OkHttpUtils 介绍、com.amazonaws.util.HttpUtils的实例源码、com.amazonaws.util.SdkHttpUtils的实例源码的知识,以帮助您更好地理解这个主题。
本文目录一览:- HttpUtils
- Android Http相关辅助类 HttpUtils
- Android 开源框架 (三) 基于 OkHttp 进一步封装的 OkHttpUtils 介绍
- com.amazonaws.util.HttpUtils的实例源码
- com.amazonaws.util.SdkHttpUtils的实例源码
HttpUtils
普通的Http工具类(IP,http请求)
package name.ealen.util;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.regex.Pattern;
/**
* Created by EalenXie on 2019/4/19 14:04.
*/
public class HttpUtils {
/**
* 获取请求网络IP
*/
public static String getIpAddress(HttpServletRequest request) {
String[] ipHeaders = {"x-forwarded-for", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};
String[] localhostIp = {"127.0.0.1", "0:0:0:0:0:0:0:1"};
String ip = request.getRemoteAddr();
for (String header : ipHeaders) {
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) break;
ip = request.getHeader(header);
}
for (String local : localhostIp) {
if (ip != null && ip.equals(local)) {
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException ignored) {
}
break;
}
}
if (ip != null && ip.length() > 15) {
if (ip.indexOf(",") > 0) ip = ip.substring(0, ip.indexOf(","));
}
return ip;
}
/**
* 检查IP格式是否合法
*/
public static boolean ipIsValid(String ip) {
if (ip == null) return false;
String regex = "(" + "(2[0-4]\\d)" + "|(25[0-5])" + ")|(" + "1\\d{2}" + ")|(" + "[1-9]\\d" + ")|(" + "\\d" + ")";
regex = "(" + regex + ").(" + regex + ").(" + regex + ").(" + regex + ")";
return Pattern.compile(regex).matcher(ip).matches();
}
/**
* 把ip转化为整数
*/
public static long translateIP2Int(String ip) {
String[] intArr = ip.split("\\.");
int[] ipInt = new int[intArr.length];
for (int i = 0; i < intArr.length; i++) {
ipInt[i] = new Integer(intArr[i]);
}
return ipInt[0] * 256 * 256 * 256 + +ipInt[1] * 256 * 256 + ipInt[2] * 256 + ipInt[3];
}
/**
* 根据Ip获取详细的地址信息,可能会出现null
*/
public static String getAddressByIp(String ip) {
if (ip == null) return null;
HttpURLConnection connection = null;
try {
URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuilder buffer = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (Exception e) {
return null;
} finally {
if (connection != null) connection.disconnect(); // 关闭连接
}
}
/**
* 向指定URL发送GET方法的请求
*/
public static String httpGet(String url, String param) throws IOException {
if (url == null) return null;
StringBuilder result = new StringBuilder();
HttpURLConnection connection;
URL realUrl = new URL(url + "?" + param);
connection = (HttpURLConnection) realUrl.openConnection();
connection.connect();
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} finally {
connection.disconnect();
}
return result.toString();
}
}
Android Http相关辅助类 HttpUtils
@H_301_0@下面是小编 jb51.cc 通过网络收集整理的代码片段。@H_301_0@小编小编现在分享给大家,也给大家做个参考。import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; /** * Http请求的工具类 * * */ public class HttpUtils { private static final int TIMEOUT_IN_MILLIONS = 5000; public interface CallBack { void onRequestComplete(String result); } /** * 异步的Get请求 * * @param urlStr * @param callBack */ public static void doGetAsyn(final String urlStr,final CallBack callBack) { new Thread() { public void run() { try { String result = doGet(urlStr); if (callBack != null) { callBack.onRequestComplete(result); } } catch (Exception e) { e.printstacktrace(); } }; }.start(); } /** * 异步的Post请求 * @param urlStr * @param params * @param callBack * @throws Exception */ public static void doPostAsyn(final String urlStr,final String params,final CallBack callBack) throws Exception { new Thread() { public void run() { try { String result = doPost(urlStr,params); if (callBack != null) { callBack.onRequestComplete(result); } } catch (Exception e) { e.printstacktrace(); } }; }.start(); } /** * Get请求,获得返回数据 * * @param urlStr * @return * @throws Exception */ public static String doGet(String urlStr) { URL url = null; HttpURLConnection conn = null; InputStream is = null; ByteArrayOutputStream baos = null; try { url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(TIMEOUT_IN_MILLIONS); conn.setConnectTimeout(TIMEOUT_IN_MILLIONS); conn.setRequestMethod("GET"); conn.setRequestProperty("accept","*/*"); conn.setRequestProperty("connection","Keep-Alive"); if (conn.getResponseCode() == 200) { is = conn.getInputStream(); baos = new ByteArrayOutputStream(); int len = -1; byte[] buf = new byte[128]; while ((len = is.read(buf)) != -1) { baos.write(buf,len); } baos.flush(); return baos.toString(); } else { throw new RuntimeException(" responseCode is not 200 ... "); } } catch (Exception e) { e.printstacktrace(); } finally { try { if (is != null) is.close(); } catch (IOException e) { } try { if (baos != null) baos.close(); } catch (IOException e) { } conn.disconnect(); } return null ; } /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 * @throws Exception */ public static String doPost(String url,String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 HttpURLConnection conn = (HttpURLConnection) realUrl .openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept","Keep-Alive"); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); conn.setRequestProperty("charset","utf-8"); conn.setUseCaches(false); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); conn.setReadTimeout(TIMEOUT_IN_MILLIONS); conn.setConnectTimeout(TIMEOUT_IN_MILLIONS); if (param != null && !param.trim().equals("")) { // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getoutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); } // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { e.printstacktrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printstacktrace(); } } return result; } }
Android 开源框架 (三) 基于 OkHttp 进一步封装的 OkHttpUtils 介绍
OkHttpUtils 是 廖子尧 是基于 OkHttp 封装的框架库。里面也封装了很多其他实用的一些组件,这里只介绍下网络相关的使用。
里面的上传下载功能使用队列的概念做了进一步封装,但是因为我使用的是旧库,对于 android6.0 运行时权限判断和 android7.0 私有文件权限设置没有处理。
同上一篇随笔一样分析:Android 开源框架 (二) 基于 OkHttp 进一步封装的 okhttp-utils 介绍 介绍下基本使用,加深自己对 OkHttp 的理解。
一。引入到自己项目后,先来对比下同上一篇介绍的 okhttp-utils 类库目录对比下:
注意:途中下面红框标注部分的 module:okhttputils-lib 是本文介绍的 OkHttpUtils 类库,上面的 module:okhttputils 是 Android 开源框架 (二) 基于 OkHttp 进一步封装的 okhttp-utils 介绍介绍的框架类库。
两个封装框架都有一个 OkHttpUtils 入口类。
二. OkHttpUtils 基本使用
1.//get 请求数据
OkHttpUtils.get(Urls.URL_METHOD)//
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new MethodCallBack<>(this, RequestInfo.class));
2.//post 请求数据
OkHttpUtils.post(Urls.URL_METHOD)//
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new MethodCallBack<>(this, RequestInfo.class));
3.// 缓存请求
OkHttpUtils.get(Urls.URL_CACHE)//
.tag(this)//
.cacheMode(CacheMode.DEFAULT)//
.cacheKey("cache_default")//
.cacheTime(5000)//对于默认的缓存模式,该时间无效,依靠的是服务端对304缓存的控制
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new CacheCallBack(this));
public enum CacheMode {
/** 按照HTTP协议的默认缓存规则,例如有304响应头时缓存 */
DEFAULT,
/** 不使用缓存 */
NO_CACHE,
/** 请求网络失败后,读取缓存 */
REQUEST_FAILED_READ_CACHE,
/** 如果缓存不存在才请求网络,否则使用缓存 */
IF_NONE_CACHE_REQUEST,
/** 先使用缓存,不管是否存在,仍然请求网络 */
FIRST_CACHE_THEN_REQUEST,
}
4.// 批量上传文件
4.1 方法一:
OkHttpUtils.post(Urls.URL_FORM_UPLOAD)//
.tag(this)//
.headers("header1", "headerValue1")//
.headers("header2", "headerValue2")//
.params("param1", "paramValue1")//
.params("param2", "paramValue2")//
// .params("file1",new File("文件路径")) //这种方式为一个key,对应一个文件
// .params("file2",new File("文件路径"))
// .params("file3",new File("文件路径"))
.addFileParams("file", files) // 这种方式为同一个key,上传多个文件
.execute(new ProgressUpCallBack<>(this, RequestInfo.class));
4.2 方式二:
for (int i = 0; i < images.size(); i++) {
//注册监听
MyUploadListener listener = new MyUploadListener();
listener.setUserTag(gridView.getChildAt(i));
//批量加入上传队列
UploadManager.getInstance(getContext()).addTask(Urls.URL_FORM_UPLOAD, new File(images.get(i).path), "imageFile", listener);
}
/** 一旦该方法执行,意味着开始下载了 */
@Override
protected UploadInfo doInBackground(Void... params) {
if (isCancelled()) return mUploadInfo;
L.e("doInBackground:" + mUploadInfo.getResourcePath());
mUploadInfo.setNetworkSpeed(0);
mUploadInfo.setState(UploadManager.UPLOADING);
postMessage(null, null, null);
//构建请求体,默认使用post请求上传
Response response;
try {
//本质还是调用OkHttpUtils的post方法上传
PostRequest postRequest = OkHttpUtils.post(mUploadInfo.getUrl());
File resource = new File(mUploadInfo.getResourcePath());
if (TextUtils.isEmpty(mUploadInfo.getFileName())) {
mUploadInfo.setFileName(resource.getName());
}
postRequest.params(mUploadInfo.getKey(), resource, mUploadInfo.getFileName());
//接口对接,数据回调
postRequest.setCallback(new MergeListener());
response = postRequest.execute();
} catch (IOException e) {
e.printStackTrace();
mUploadInfo.setNetworkSpeed(0);
mUploadInfo.setState(UploadManager.ERROR);
postMessage(null, "网络异常", e);
return mUploadInfo;
}
if (response.isSuccessful()) {
//解析过程中抛出异常,一般为 json 格式错误,或者数据解析异常
try {
T t = (T) mUploadInfo.getListener().parseNetworkResponse(response);
mUploadInfo.setNetworkSpeed(0);
mUploadInfo.setState(UploadManager.FINISH); //上传成功
postMessage(t, null, null);
return mUploadInfo;
} catch (Exception e) {
e.printStackTrace();
mUploadInfo.setNetworkSpeed(0);
mUploadInfo.setState(UploadManager.ERROR);
postMessage(null, "解析数据对象出错", e);
return mUploadInfo;
}
} else {
mUploadInfo.setNetworkSpeed(0);
mUploadInfo.setState(UploadManager.ERROR);
postMessage(null, "数据返回失败", null);
return mUploadInfo;
}
}
5.// 批量下载文件
5.1 方式一:
OkHttpUtils.get(Urls.URL_DOWNLOAD)//
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DownloadFileCallBack(Environment.getExternalStorageDirectory() + "/temp", "OkHttpUtils.apk"));
5.2 方式二(DownloadManager):
if (downloadManager.getTaskByUrl(apk.getUrl()) != null) {
Toast.makeText(getContext(), "任务已经在下载列表中", Toast.LENGTH_SHORT).show();
} else {
downloadManager.addTask(apk.getUrl(), null);
AppCacheUtils.getInstance(getContext()).put(apk.getUrl(), apk);
download.setText("已在队列");
download.setEnabled(false);
}
/** 一旦该方法执行,意味着开始下载了 */
@Override
protected DownloadInfo doInBackground(Void... params) {
if (isCancelled()) return mDownloadInfo;
L.e("doInBackground:" + mDownloadInfo.getFileName());
mPreviousTime = System.currentTimeMillis();
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.DOWNLOADING);
postMessage(null, null);
//构建下载文件路径,如果有设置,就用设置的,否者就自己创建
String url = mDownloadInfo.getUrl();
String fileName = mDownloadInfo.getFileName();
if (TextUtils.isEmpty(fileName)) {
fileName = getUrlFileName(url);
mDownloadInfo.setFileName(fileName);
}
if (TextUtils.isEmpty(mDownloadInfo.getTargetPath())) {
File file = new File(mDownloadInfo.getTargetFolder(), fileName);
mDownloadInfo.setTargetPath(file.getAbsolutePath());
}
//检查手机上文件的有效性
File file = new File(mDownloadInfo.getTargetPath());
long startPos;
if (file.length() != mDownloadInfo.getDownloadLength()) {
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.ERROR);
postMessage("断点文件异常,需要删除后重新下载", null);
return mDownloadInfo;
} else {
//断点下载的情况
startPos = mDownloadInfo.getDownloadLength();
}
//再次检查文件有效性,文件大小大于总文件大小
if (startPos > mDownloadInfo.getTotalLength()) {
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.ERROR);
postMessage("断点文件异常,需要删除后重新下载", null);
return mDownloadInfo;
}
if (startPos == mDownloadInfo.getTotalLength() && startPos > 0) {
mDownloadInfo.setProgress(1.0f);
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.FINISH);
postMessage(null, null);
return mDownloadInfo;
}
//设置断点写文件
ProgressRandomAccessFile randomAccessFile;
try {
randomAccessFile = new ProgressRandomAccessFile(file, "rw", startPos);
} catch (FileNotFoundException e) {
e.printStackTrace();
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.ERROR);
postMessage("没有找到已存在的断点文件", e);
return mDownloadInfo;
}
L.e("startPos:" + startPos + " path:" + mDownloadInfo.getTargetPath());
//构建请求体,默认使用get请求下载,设置断点头
Response response;
try {
response = OkHttpUtils.get(url).headers("RANGE", "bytes=" + startPos + "-").execute();
} catch (IOException e) {
e.printStackTrace();
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.ERROR);
postMessage("网络异常", e);
return mDownloadInfo;
}
//获取流对象,准备进行读写文件
long totalLength = response.body().contentLength();
if (mDownloadInfo.getTotalLength() == 0) {
mDownloadInfo.setTotalLength(totalLength);
}
InputStream is = response.body().byteStream();
//读写文件流
try {
download(is, randomAccessFile);
} catch (IOException e) {
e.printStackTrace();
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.ERROR);
postMessage("文件读写异常", e);
return mDownloadInfo;
}
//循环结束走到这里,a.下载完成 b.暂停 c.判断是否下载出错
if (isCancelled()) {
L.e("state: 暂停" + mDownloadInfo.getState());
mDownloadInfo.setNetworkSpeed(0);
if (isPause) mDownloadInfo.setState(DownloadManager.PAUSE); //暂停
else mDownloadInfo.setState(DownloadManager.NONE); //停止
postMessage(null, null);
} else if (file.length() == mDownloadInfo.getTotalLength() && mDownloadInfo.getState() == DownloadManager.DOWNLOADING) {
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.FINISH); //下载完成
postMessage(null, null);
} else if (file.length() != mDownloadInfo.getDownloadLength()) {
mDownloadInfo.setNetworkSpeed(0);
mDownloadInfo.setState(DownloadManager.ERROR); //由于不明原因,文件保存有误
postMessage("未知原因", null);
}
return mDownloadInfo;
}
com.amazonaws.util.HttpUtils的实例源码
@Override public String buildrequestUrl() { String url = this.endpoint.toString(); StringBuilder builder = new StringBuilder(url); if (!url.endsWith("/")) { builder.append("/"); } String timestamp = Utilities.getTimestamp(); String signature = Utilities .getSignature(timestamp,this.decryptionKey); builder.append("login"); builder.append("?uid=" + HttpUtils.urlEncode(this.uid,false)); builder.append("&username=" + HttpUtils.urlEncode(this.username,false)); builder.append("×tamp=" + HttpUtils.urlEncode(timestamp,false)); builder.append("&signature=" + HttpUtils.urlEncode(signature,false)); return builder.toString(); }
public String buildrequestUrl() { StringBuilder builder = new StringBuilder( ( this.useSSL ? "https://" : "http://" ) ); builder.append( this.endpoint ); builder.append( "/" ); builder.append( "registerdevice" ); builder.append( "?uid=" + HttpUtils.urlEncode( this.uid,false ) ); builder.append( "&key=" + HttpUtils.urlEncode( this.key,false ) ); return builder.toString(); }
public String buildrequestUrl() { StringBuilder builder = new StringBuilder( ( this.useSSL ? "https://" : "http://" ) ); builder.append( this.endpoint ); builder.append( "/" ); String timestamp = Utilities.getTimestamp(); String signature = Utilities.getSignature( timestamp,key ); builder.append( "gettoken" ); builder.append( "?uid=" + HttpUtils.urlEncode( this.uid,false ) ); builder.append( "×tamp=" + HttpUtils.urlEncode( timestamp,false ) ); builder.append( "&signature=" + HttpUtils.urlEncode( signature,false ) ); return builder.toString(); }
public static String encode(String s) { if (null == s) return s; return HttpUtils.urlEncode(s,false); }
public static String encode(String s) { if (null == s) return s; return HttpUtils.urlEncode(s,false); }
@Override public String buildrequestUrl() { String url = this.endpoint.toString(); StringBuilder builder = new StringBuilder(url); if (!url.endsWith("/")) { builder.append("/"); } String timestamp = Utilities.getTimestamp(); builder.append("gettoken"); builder.append("?uid=" + HttpUtils.urlEncode(this.uid,false)); int counter = 1; StringBuilder loginString = new StringBuilder(); for (Map.Entry<String,String> entry : logins.entrySet()) { loginString.append(entry.getKey() + entry.getValue()); builder.append("&provider"); builder.append(counter); builder.append("="); builder.append(HttpUtils.urlEncode(entry.getKey(),false)); builder.append("&token"); builder.append(counter); builder.append("="); builder.append(HttpUtils.urlEncode(entry.getValue(),false)); counter++; } builder.append("&identityId=" + HttpUtils.urlEncode(identityId,false)); String signature = null; if (identityId != null) { signature = Utilities.getSignature(timestamp + loginString + identityId,key); } else { signature = Utilities.getSignature(timestamp + loginString,key); } builder.append("&signature=" + HttpUtils.urlEncode(signature,false)); return builder.toString(); }
com.amazonaws.util.SdkHttpUtils的实例源码
private String urlEncodeTags(ObjectTagging tagging) { if (tagging == null || tagging.getTagSet() == null) return null; StringBuilder sb = new StringBuilder(); Iterator<Tag> tagIter = tagging.getTagSet().iterator(); while (tagIter.hasNext()) { Tag tag = tagIter.next(); sb.append(SdkHttpUtils.urlEncode(tag.getKey(),false)).append('=').append(SdkHttpUtils.urlEncode(tag.getValue(),false)); if (tagIter.hasNext()) { sb.append("&"); } } return sb.toString(); }
/** * Set the request's endpoint and resource path with the new region provided * * @param request Request to set endpoint for * @param regionString New region to determine endpoint to hit */ public void resolveRequestEndpoint(Request<?> request,String regionString) { if (regionString != null) { final Region r = RegionUtils.getRegion(regionString); if (r == null) { throw new SdkClientException("Not able to determine region" + " for " + regionString + ".Please upgrade to a newer " + "version of the SDK"); } endpointBuilder.withRegion(r); } final URI endpoint = endpointBuilder.getServiceEndpoint(); if (shouldUseVirtualAddressing(endpoint)) { request.setEndpoint(convertToVirtualHostEndpoint(endpoint,bucketName)); request.setResourcePath(SdkHttpUtils.urlEncode(getHostStyleResourcePath(),true)); } else { request.setEndpoint(endpoint); if (bucketName != null) { request.setResourcePath(SdkHttpUtils.urlEncode(getPathStyleResourcePath(),true)); } } }
public Request apply(SignableRequest<?> signableRequest) { Multimap<String,String> headers = ArrayListMultimap.create(Multimaps.forMap(signableRequest.getHeaders())); headers.put(HttpHeaders.Names.CONTENT_TYPE,HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED); return new RequestBuilder() .setMethod(signableRequest.getHttpMethod().name()) .setUrl(signableRequest.getEndpoint().resolve(signableRequest.getResourcePath()).toString()) .setHeaders(headers.asMap()) .setBody(SdkHttpUtils.encodeParameters(signableRequest)) .build(); }
@Override public String marshall(String resourcePath,String paramName,String pathValue) { if (pathValue != null && pathValue.isEmpty()) { throw new IllegalArgumentException(paramName + " must not be empty. If not set a value will be auto generated"); } return resourcePath.replace(String.format("{%s}",paramName),SdkHttpUtils.urlEncode(IdempotentUtils.resolveString(pathValue),false)); }
@Override public HttpRequestBase create(final Request<?> request,final HttpClientSettings settings) throws FakeIOException { URI endpoint = request.getEndpoint(); /* * HttpClient cannot handle url in pattern of "http://host//path",so we * have to escape the double-slash between endpoint and resource-path * into "/%2F" */ String uri = SdkHttpUtils.appendUri(endpoint.toString(),request .getResourcePath(),true); String encodedParams = SdkHttpUtils.encodeParameters(request); /* * For all non-POST requests,and any POST requests that already have a * payload,we put the encoded params directly in the URI,otherwise,* we'll put them in the POST request's payload. */ boolean requestHasnopayload = request.getContent() != null; boolean requestIsPost = request.getHttpMethod() == HttpMethodName.POST; boolean putParamsInUri = !requestIsPost || requestHasnopayload; if (encodedParams != null && putParamsInUri) { uri += "?" + encodedParams; } final HttpRequestBase base = createApacheRequest(request,uri,encodedParams); addHeadersToRequest(base,request); addRequestConfig(base,request,settings); return base; }
private String getHostHeaderValue(final URI endpoint) { /* * Apache HttpClient omits the port number in the Host header (even if * we explicitly specify it) if it's the default port for the protocol * in use. To ensure that we use the same Host header in the request and * in the calculated string to sign (even if Apache HttpClient changed * and started honoring our explicit host with endpoint),we follow this * same behavior here and in the QueryString signer. */ return SdkHttpUtils.isUsingNonDefaultPort(endpoint) ? endpoint.getHost() + ":" + endpoint.getPort() : endpoint.getHost(); }
/** * Step 1 of the AWS Signature version 4 calculation. Refer to * http://docs.aws * .amazon.com/general/latest/gr/sigv4-create-canonical-request.html to * generate the canonical request. */ protected String createCanonicalRequest(SignableRequest<?> request,String contentSha256) { /* This would url-encode the resource path for the first time. */ final String path = SdkHttpUtils.appendUri( request.getEndpoint().getPath(),request.getResourcePath()); final StringBuilder canonicalRequestBuilder = new StringBuilder(request .getHttpMethod().toString()); canonicalRequestBuilder.append(LINE_SEParaTOR) // This would optionally double url-encode the resource path .append(getCanonicalizedResourcePath(path,doubleurlEncode)) .append(LINE_SEParaTOR) .append(getCanonicalizedQueryString(request)) .append(LINE_SEParaTOR) .append(getCanonicalizedHeaderString(request)) .append(LINE_SEParaTOR) .append(getSignedHeadeRSString(request)).append(LINE_SEParaTOR) .append(contentSha256); final String canonicalRequest = canonicalRequestBuilder.toString(); if (log.isDebugEnabled()) log.debug("AWS4 Canonical Requests: '\"" + canonicalRequest + "\""); return canonicalRequest; }
protected void addHostHeader(SignableRequest<?> request) { // AWS4 requires that we sign the Host header so we // have to have it in the request by the time we sign. final URI endpoint = request.getEndpoint(); final StringBuilder hostHeaderBuilder = new StringBuilder( endpoint.getHost()); if (SdkHttpUtils.isUsingNonDefaultPort(endpoint)) { hostHeaderBuilder.append(":").append(endpoint.getPort()); } request.addHeader(HOST,hostHeaderBuilder.toString()); }
protected String getCanonicalizedQueryString(SignableRequest<?> request) { /* * If we're using POST and we don't have any request payload content,* then any request query parameters will be sent as the payload,and * not in the actual query string. */ if (SdkHttpUtils.usePayloadForQueryParameters(request)) return ""; return this.getCanonicalizedQueryString(request.getParameters()); }
/** * Returns the request's payload as binary data. * * @param request * The request * @return The data from the request's payload,as binary data. */ protected byte[] getBinaryRequestPayload(SignableRequest<?> request) { if (SdkHttpUtils.usePayloadForQueryParameters(request)) { String encodedParameters = SdkHttpUtils.encodeParameters(request); if (encodedParameters == null) return new byte[0]; return encodedParameters.getBytes(UTF8); } return getBinaryRequestPayloadWithoutQueryParams(request); }
protected InputStream getBinaryRequestPayloadStream(SignableRequest<?> request) { if (SdkHttpUtils.usePayloadForQueryParameters(request)) { String encodedParameters = SdkHttpUtils.encodeParameters(request); if (encodedParameters == null) return new ByteArrayInputStream(new byte[0]); return new ByteArrayInputStream( encodedParameters.getBytes(UTF8)); } return getBinaryRequestPayloadStreamWithoutQueryParams(request); }
protected String getCanonicalizedResourcePath(String resourcePath,boolean urlEncode) { if (resourcePath == null || resourcePath.isEmpty()) { return "/"; } else { String value = urlEncode ? SdkHttpUtils.urlEncode(resourcePath,true) : resourcePath; if (value.startsWith("/")) { return value; } else { return "/".concat(value); } } }
protected String getCanonicalizedEndpoint(URI endpoint) { String endpointForStringToSign = StringUtils.lowerCase(endpoint.getHost()); /* * Apache HttpClient will omit the port in the Host header for default * port values (i.e. 80 for HTTP and 443 for HTTPS) even if we * explicitly specify it,so we need to be careful that we use the same * value here when we calculate the string to sign and in the Host * header we send in the HTTP request. */ if (SdkHttpUtils.isUsingNonDefaultPort(endpoint)) { endpointForStringToSign += ":" + endpoint.getPort(); } return endpointForStringToSign; }
/** * Pre-signs the specified request,using a signature query-string * parameter. * * @param request * The request to sign. * @param methodName * The HTTP method (GET,PUT,DELETE,HEAD) for the specified * request. * @param bucketName * The name of the bucket involved in the request. If the request * is not an operation on a bucket this parameter should be null. * @param key * The object key involved in the request. If the request is not * an operation on an object,this parameter should be null. * @param expiration * The time at which the signed request is no longer valid,and * will stop working. * @param subResource * The optional sub-resource being requested as part of the * request (e.g. "location","acl","logging",or "torrent"). */ protected <T> void presignRequest(Request<T> request,HttpMethod methodName,String bucketName,String key,Date expiration,String subResource) { // Run any additional request handlers if present beforeRequest(request); String resourcePath = "/" + ((bucketName != null) ? bucketName + "/" : "") + ((key != null) ? SdkHttpUtils.urlEncode(key,true) : "") + ((subResource != null) ? "?" + subResource : ""); // Make sure the resource-path for signing does not contain // any consecutive "/"s. // Note that we should also follow the same rule to escape // consecutive "/"s when generating the presigned URL. // See ServiceUtils#convertRequestToUrl(...) resourcePath = resourcePath.replaceAll("(?<=/)/","%2F"); new S3QueryStringSigner(methodName.toString(),resourcePath,expiration) .sign(request,CredentialUtils.getCredentialsProvider(request.getoriginalRequest(),awsCredentialsProvider).getCredentials()); // The Amazon S3 DevPay token header is a special exception and can be safely moved // from the request's headers into the query string to ensure that it travels along // with the pre-signed URL when it's sent back to Amazon S3. if (request.getHeaders().containsKey(Headers.Security_TOKEN)) { String value = request.getHeaders().get(Headers.Security_TOKEN); request.addParameter(Headers.Security_TOKEN,value); request.getHeaders().remove(Headers.Security_TOKEN); } }
/** * <p> * Populates the specified request with the numerous options available in * <code>copyObjectRequest</code>. * </p> * * @param request * The request to populate with headers to represent all the * options expressed in the <code>copyPartRequest</code> object. * @param copyPartRequest * The object containing all the options for copying an object in * Amazon S3. */ private static void populateRequestWithcopyPartParameters(Request<?> request,copyPartRequest copyPartRequest) { String copySourceHeader = "/" + SdkHttpUtils.urlEncode(copyPartRequest.getSourceBucketName(),true) + "/" + SdkHttpUtils.urlEncode(copyPartRequest.getSourceKey(),true); if (copyPartRequest.getSourceVersionId() != null) { copySourceHeader += "?versionId=" + copyPartRequest.getSourceVersionId(); } request.addHeader("x-amz-copy-source",copySourceHeader); addDateHeader(request,Headers.copY_SOURCE_IF_MODIFIED_SINCE,copyPartRequest.getModifiedSinceConstraint()); addDateHeader(request,Headers.copY_SOURCE_IF_UNMODIFIED_SINCE,copyPartRequest.getUnmodifiedSinceConstraint()); addStringListHeader(request,Headers.copY_SOURCE_IF_MATCH,copyPartRequest.getMatchingETagConstraints()); addStringListHeader(request,Headers.copY_SOURCE_IF_NO_MATCH,copyPartRequest.getNonmatchingETagConstraints()); if ( copyPartRequest.getFirstByte() != null && copyPartRequest.getLastByte() != null ) { String range = "bytes=" + copyPartRequest.getFirstByte() + "-" + copyPartRequest.getLastByte(); request.addHeader(Headers.copY_PART_RANGE,range); } // Populate the SSE-C parameters for the destination object populateSourceSSE_C(request,copyPartRequest.getSourceSSECustomerKey()); populateSSE_C(request,copyPartRequest.getDestinationSSECustomerKey()); }
private String queryParamsstring(Multimap<String,String> queryParams) { final ImmutableList.Builder<String> result = ImmutableList.builder(); for (Map.Entry<String,Collection<String>> param : new TreeMap<>(queryParams.asMap()).entrySet()) { for (String value : param.getValue()) { result.add(SdkHttpUtils.urlEncode(param.getKey(),false) + '=' + SdkHttpUtils.urlEncode(value,false)); } } return AMPERSAND_JOINER.join(result.build()); }
private Request<?> buildAWSRequest(String httpMethod,String endpoint,String resourcePath,Map<String,String> headers,String> params,InputStream entity) { Request<AmazonWebServiceRequest> r = new DefaultRequest<>(Config.Para); if (!StringUtils.isBlank(httpMethod)) { r.setHttpMethod(HttpMethodName.valueOf(httpMethod)); } if (!StringUtils.isBlank(endpoint)) { if (!endpoint.startsWith("http")) { endpoint = "https://" + endpoint; } r.setEndpoint(URI.create(endpoint)); } if (!StringUtils.isBlank(resourcePath)) { r.setResourcePath(SdkHttpUtils.urlEncode(resourcePath,true)); } if (headers != null) { if (headers.containsKey("x-amz-date")) { overriddenDate = parseAWSDate(headers.get("x-amz-date")); } // we don't need these here,added by default headers.remove("host"); headers.remove("x-amz-date"); r.setHeaders(headers); } if (params != null) { for (Map.Entry<String,String> param : params.entrySet()) { r.addParameter(param.getKey(),param.getValue()); } } if (entity != null) { r.setContent(entity); } return r; }
@Override public String marshall(String resourcePath,String pathValue) { assertStringNotEmpty(pathValue,paramName); return resourcePath.replace(String.format("{%s}",SdkHttpUtils.urlEncode(pathValue,false)); }
/** * Signs the specified request with the AWS3 signing protocol by using the * AWS account credentials specified when this object was constructed and * adding the required AWS3 headers to the request. * * @param request * The request to sign. */ @Override public void sign(SignableRequest<?> request,AWSCredentials credentials) throws SdkClientException { // annonymous credentials,don't sign if ( credentials instanceof AnonymousAWSCredentials ) { return; } AWSCredentials sanitizedCredentials = sanitizeCredentials(credentials); Signingalgorithm algorithm = Signingalgorithm.HmacSHA256; String nonce = UUID.randomUUID().toString(); int timeOffset = request.getTimeOffset(); Date dateValue = getSignatureDate(timeOffset); String date = DateUtils.formatRFC822Date(dateValue); boolean isHttps = false; if (overriddenDate != null) date = overriddenDate; request.addHeader("Date",date); request.addHeader("X-Amz-Date",date); // AWS3 HTTP requires that we sign the Host header // so we have to have it in the request by the time we sign. String hostHeader = request.getEndpoint().getHost(); if (SdkHttpUtils.isUsingNonDefaultPort(request.getEndpoint())) { hostHeader += ":" + request.getEndpoint().getPort(); } request.addHeader("Host",hostHeader); if ( sanitizedCredentials instanceof AWSSessionCredentials ) { addSessionCredentials(request,(AWSSessionCredentials) sanitizedCredentials); } byte[] bytesToSign; String stringToSign; if (isHttps) { request.addHeader(NONCE_HEADER,nonce); stringToSign = date + nonce; bytesToSign = stringToSign.getBytes(UTF8); } else { String path = SdkHttpUtils.appendUri(request.getEndpoint().getPath(),request.getResourcePath()); /* * AWS3 requires all query params to be listed on the third line of * the string to sign,even if those query params will be sent in * the request body and not as a query string. POST formatted query * params should *NOT* be included in the request payload. */ stringToSign = request.getHttpMethod().toString() + "\n" + getCanonicalizedResourcePath(path) + "\n" + getCanonicalizedQueryString(request.getParameters()) + "\n" + getCanonicalizedHeadersForStringToSign(request) + "\n" + getRequestPayloadWithoutQueryParams(request); bytesToSign = hash(stringToSign); } if (log.isDebugEnabled()) log.debug("Calculated StringToSign: " + stringToSign); String signature = signAndBase64Encode(bytesToSign,sanitizedCredentials.getAWSSecretKey(),algorithm); StringBuilder builder = new StringBuilder(); builder.append(isHttps ? HTTPS_SCHEME : HTTP_SCHEME).append(" "); builder.append("AWSAccessKeyId=" + sanitizedCredentials.getAWSAccessKeyId() + ","); builder.append("Algorithm=" + algorithm.toString() + ","); if (!isHttps) { builder.append(getSignedHeadersComponent(request) + ","); } builder.append("Signature=" + signature); request.addHeader(AUTHORIZATION_HEADER,builder.toString()); }
/** * <p> * Populates the specified request with the numerous options available in * <code>copyObjectRequest</code>. * </p> * * @param request * The request to populate with headers to represent all the * options expressed in the <code>copyObjectRequest</code> object. * @param copyObjectRequest * The object containing all the options for copying an object in * Amazon S3. */ private void populateRequestWithcopyObjectParameters(Request<? extends AmazonWebServiceRequest> request,copyObjectRequest copyObjectRequest) { String copySourceHeader = "/" + SdkHttpUtils.urlEncode(copyObjectRequest.getSourceBucketName(),true) + "/" + SdkHttpUtils.urlEncode(copyObjectRequest.getSourceKey(),true); if (copyObjectRequest.getSourceVersionId() != null) { copySourceHeader += "?versionId=" + copyObjectRequest.getSourceVersionId(); } request.addHeader("x-amz-copy-source",copyObjectRequest.getModifiedSinceConstraint()); addDateHeader(request,copyObjectRequest.getUnmodifiedSinceConstraint()); addStringListHeader(request,copyObjectRequest.getMatchingETagConstraints()); addStringListHeader(request,copyObjectRequest.getNonmatchingETagConstraints()); if (copyObjectRequest.getAccessControlList() != null) { addAclHeaders(request,copyObjectRequest.getAccessControlList()); } else if (copyObjectRequest.getCannedAccessControlList() != null) { request.addHeader(Headers.S3_CANNED_ACL,copyObjectRequest.getCannedAccessControlList().toString()); } if (copyObjectRequest.getStorageClass() != null) { request.addHeader(Headers.STORAGE_CLASS,copyObjectRequest.getStorageClass()); } if (copyObjectRequest.getRedirectLocation() != null) { request.addHeader(Headers.REDIRECT_LOCATION,copyObjectRequest.getRedirectLocation()); } populateRequesterPaysHeader(request,copyObjectRequest.isRequesterPays()); ObjectMetadata newObjectMetadata = copyObjectRequest.getNewObjectMetadata(); if (newObjectMetadata != null) { request.addHeader(Headers.Metadata_dirECTIVE,"REPLACE"); populateRequestMetadata(request,newObjectMetadata); } ObjectTagging newObjectTagging = copyObjectRequest.getNewObjectTagging(); if (newObjectTagging != null) { request.addHeader(Headers.TAGGING_DIRECTIVE,"REPLACE"); request.addHeader(Headers.S3_TAGGING,urlEncodeTags(newObjectTagging)); } // Populate the SSE-C parameters for the destination object populateSourceSSE_C(request,copyObjectRequest.getSourceSSECustomerKey()); populateSSE_C(request,copyObjectRequest.getDestinationSSECustomerKey()); }
/** * Converts the specified request object into a URL,containing all the * specified parameters,the specified request endpoint,etc. * * @param request * The request to convert into a URL. * @param removeLeadingSlashInResourcePath * Whether the leading slash in resource-path should be removed * before appending to the endpoint. * @param urlEncode True if request resource path should be URL encoded * @return A new URL representing the specified request. * * @throws SdkClientException * If the request cannot be converted to a well formed URL. */ public static URL convertRequestToUrl(Request<?> request,boolean removeLeadingSlashInResourcePath,boolean urlEncode) { String resourcePath = urlEncode ? SdkHttpUtils.urlEncode(request.getResourcePath(),true) : request.getResourcePath(); // Removed the padding "/" that was already added into the request's resource path. if (removeLeadingSlashInResourcePath && resourcePath.startsWith("/")) { resourcePath = resourcePath.substring(1); } // Some http client libraries (e.g. Apache HttpClient) cannot handle // consecutive "/"s between URL authority and path components. // So we escape "////..." into "/%2F%2F%2F...",in the same way as how // we treat consecutive "/"s in AmazonS3Client#presignRequest(...) String urlPath = "/" + resourcePath; urlPath = urlPath.replaceAll("(?<=/)/","%2F"); StringBuilder url = new StringBuilder(request.getEndpoint().toString()); url.append(urlPath); StringBuilder queryParams = new StringBuilder(); Map<String,List<String>> requestParams = request.getParameters(); for (Map.Entry<String,List<String>> entry : requestParams.entrySet()) { for (String value : entry.getValue()) { queryParams = queryParams.length() > 0 ? queryParams .append("&") : queryParams.append("?"); queryParams.append(entry.getKey()) .append("=") .append(SdkHttpUtils.urlEncode(value,false)); } } url.append(queryParams.toString()); try { return new URL(url.toString()); } catch (MalformedURLException e) { throw new SdkClientException( "Unable to convert request to well formed URL: " + e.getMessage(),e); } }
@Override public void sign(SignableRequest<?> request,AWSCredentials credentials) { if (resourcePath == null) { throw new UnsupportedOperationException( "Cannot sign a request using a dummy S3Signer instance with " + "no resource path"); } if (credentials == null || credentials.getAWSSecretKey() == null) { log.debug("Canonical string will not be signed,as no AWS Secret Key was provided"); return; } AWSCredentials sanitizedCredentials = sanitizeCredentials(credentials); if (sanitizedCredentials instanceof AWSSessionCredentials) { addSessionCredentials(request,(AWSSessionCredentials) sanitizedCredentials); } /* * In s3 sigv2,the way slash characters are encoded should be * consistent in both the request url and the encoded resource path. * Since we have to encode "//" to "/%2F" in the request url to make * httpclient works,we need to do the same encoding here for the * resource path. */ String encodedResourcePath = SdkHttpUtils.appendUri( request.getEndpoint().getPath(),SdkHttpUtils.urlEncode(resourcePath,true),true); int timeOffset = request.getTimeOffset(); Date date = getSignatureDate(timeOffset); request.addHeader(Headers.DATE,ServiceUtils.formatRfc822Date(date)); String canonicalString = RestUtils.makeS3CanonicalString(httpVerb,encodedResourcePath,null,additionalQueryParamsToSign); log.debug("Calculated string to sign:\n\"" + canonicalString + "\""); String signature = super.signAndBase64Encode(canonicalString,Signingalgorithm.HmacSHA1); request.addHeader("Authorization","AWS " + sanitizedCredentials.getAWSAccessKeyId() + ":" + signature); }
public Map<String,Object> getSignedHeaders(String uri,String method,Multimap<String,String> queryParams,Object> headers,Optional<byte[]> payload) { final LocalDateTime Now = clock.get(); final AWSCredentials credentials = credentialsProvider.getCredentials(); final Map<String,Object> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); result.putAll(headers); final Optional<String> possibleHost = Optional.fromNullable(result.get(HOST)) .transform(Object::toString); final int indexOfPortSymbol = possibleHost.transform(host -> host.indexOf(':')).or(-1); if (indexOfPortSymbol > -1) { result.put(HOST,possibleHost.get().substring(0,indexOfPortSymbol)); } if (!result.containsKey(DATE)) { result.put(X_AMZ_DATE,Now.format(BASIC_TIME_FORMAT)); } if (AWSSessionCredentials.class.isAssignableFrom(credentials.getClass())) { result.put(SESSION_TOKEN,((AWSSessionCredentials) credentials).getSessionToken()); } final StringBuilder headeRSString = new StringBuilder(); final ImmutableList.Builder<String> signedHeaders = ImmutableList.builder(); for (Map.Entry<String,Object> entry : result.entrySet()) { final Optional<String> headerAsstring = headerAsstring(entry,method); if (headerAsstring.isPresent()) { headeRSString.append(headerAsstring.get()).append(RETURN); signedHeaders.add(entry.getKey().toLowerCase()); } } final String signedHeaderKeys = JOINER.join(signedHeaders.build()); final String canonicalRequest = method + RETURN + SdkHttpUtils.urlEncode(uri,true) + RETURN + queryParamsstring(queryParams) + RETURN + headeRSString.toString() + RETURN + signedHeaderKeys + RETURN + toBase16(hash(payload.or(EMPTY.getBytes(Charsets.UTF_8)))); final String stringToSign = createStringToSign(canonicalRequest,Now); final String signature = sign(stringToSign,Now,credentials); final String autorizationHeader = AWS4_HMAC_SHA256_CREDENTIAL + credentials.getAWSAccessKeyId() + SLASH + getCredentialScope(Now) + SIGNED_HEADERS + signedHeaderKeys + SIGNATURE + signature; result.put(AUTHORIZATION,autorizationHeader); return ImmutableMap.copyOf(result); }
/** * Perform a url decode on the given value if specified. * Return value by default; */ private static String decodeIfSpecified(String value,boolean decode) { return decode ? SdkHttpUtils.urlDecode(value) : value; }
/** * S3 URL encodes the key of the object involved in the event. This is * a convenience method to automatically URL decode the key. * @return The URL decoded object key. */ public String getUrlDecodedKey() { return SdkHttpUtils.urlDecode(getKey()); }
今天关于HttpUtils的分享就到这里,希望大家有所收获,若想了解更多关于Android Http相关辅助类 HttpUtils、Android 开源框架 (三) 基于 OkHttp 进一步封装的 OkHttpUtils 介绍、com.amazonaws.util.HttpUtils的实例源码、com.amazonaws.util.SdkHttpUtils的实例源码等相关知识,可以在本站进行查询。
本文标签: