public Response send(final Request.Builder requestBuilder, StringMap headers) throws QiniuException { if (headers != null) { headers.forEach( new StringMap.Consumer() { @Override public void accept(String key, Object value) { requestBuilder.header(key, value.toString()); } }); } requestBuilder.header("User-Agent", userAgent()); long start = System.currentTimeMillis(); com.squareup.okhttp.Response res = null; Response r; double duration = (System.currentTimeMillis() - start) / 1000.0; IpTag tag = new IpTag(); try { res = httpClient.newCall(requestBuilder.tag(tag).build()).execute(); } catch (IOException e) { e.printStackTrace(); throw new QiniuException(e); } r = Response.create(res, tag.ip, duration); if (r.statusCode >= 300) { throw new QiniuException(r); } return r; }
private String fileUrl() { String url = host + "/mkfile/" + size + "/mimeType/" + UrlSafeBase64.encodeToString(mime) + "/fname/" + UrlSafeBase64.encodeToString(f.getName()); final StringBuilder b = new StringBuilder(url); if (key != null) { b.append("/key/"); b.append(UrlSafeBase64.encodeToString(key)); } if (params != null) { params.forEach( new StringMap.Consumer() { public void accept(String key, Object value) { b.append("/"); b.append(key); b.append("/"); b.append(value); } }); } return b.toString(); }
public Response post(String url, StringMap params, StringMap headers) throws QiniuException { final FormEncodingBuilder f = new FormEncodingBuilder(); params.forEach( new StringMap.Consumer() { @Override public void accept(String key, Object value) { f.add(key, value.toString()); } }); return post(url, f.build(), headers); }
private Response multipartPost( String url, StringMap fields, String name, String fileName, RequestBody file, StringMap headers) throws QiniuException { final MultipartBuilder mb = new MultipartBuilder(); mb.addFormDataPart(name, fileName, file); fields.forEach( new StringMap.Consumer() { @Override public void accept(String key, Object value) { mb.addFormDataPart(key, value.toString()); } }); mb.type(MediaType.parse("multipart/form-data")); RequestBody body = mb.build(); Request.Builder requestBuilder = new Request.Builder().url(url).post(body); return send(requestBuilder, headers); }