本文整理汇总了Java中com.mashape.unirest.request.HttpRequestWithBody.headers方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestWithBody.headers方法的具体用法?Java HttpRequestWithBody.headers怎么用?Java HttpRequestWithBody.headers使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mashape.unirest.request.HttpRequestWithBody
的用法示例。
在下文中一共展示了HttpRequestWithBody.headers方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: download
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
@Override
public HttpResponse download(HttpRequest request) throws DownloaderException {
if(log.isDebugEnabled()) {
log.debug("downloading..." + request.getUrl());
}
try {
HttpHost proxy = Proxys.getProxy();
if(proxy != null) {
Unirest.setProxy(proxy);
} else {
Unirest.setProxy(null);
}
request.addHeader("User-Agent", UserAgent.getUserAgent());
com.mashape.unirest.http.HttpResponse<String> response = null;
if(request instanceof HttpPostRequest) {
HttpPostRequest post = (HttpPostRequest)request;
HttpRequestWithBody httpRequestWithBody = Unirest.post(post.getUrl());
httpRequestWithBody.headers(post.getHeaders());
httpRequestWithBody.fields(post.getFields());
response = httpRequestWithBody.asString();
} else {
response = Unirest.get(request.getUrl()).headers(request.getHeaders()).asString();
}
String contentType = response.getHeaders().getFirst("Content-Type");
HttpResponse resp = new HttpResponse();
resp.setStatus(response.getStatus());
resp.setRaw(response.getRawBody());
resp.setContent(response.getBody());
resp.setContentType(contentType);
resp.setCharset(getCharset(request, contentType));
return resp;
} catch (UnirestException e) {
throw new DownloaderException(e);
}
}
示例2: post
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Make a [POST]
* @param url
* @param data
* @param authentication
* @return resposne Code
* @throws UnirestException
*/
public int post(String url, Map<String, Object> data, boolean authentication) throws UnirestException{
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
if (authentication) {
header.put("Authorization", "Token " + token);
}
HttpRequestWithBody h = Unirest.post(getAPIURL(url));
h.headers(header);
h.fields(data);
if (url.equals("login/")) {
try {
JSONObject j = h.asJson().getBody().getObject();
this.token = j.get("token").toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
return h.asString().getCode();
}
示例3: patch
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Make a [PATCH]
* @param url
* @param data
* @return response Code
* @throws UnirestException
*/
public int patch(String url, Map<String, Object> data) throws UnirestException {
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
header.put("Authorization", "Token " + token);
HttpRequestWithBody h = Unirest.patch(getAPIURL(url));
h.headers(header);
h.fields(data);
return h.asJson().getCode();
}