本文整理汇总了Java中com.mashape.unirest.request.HttpRequestWithBody.asString方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestWithBody.asString方法的具体用法?Java HttpRequestWithBody.asString怎么用?Java HttpRequestWithBody.asString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mashape.unirest.request.HttpRequestWithBody
的用法示例。
在下文中一共展示了HttpRequestWithBody.asString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: save
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
@Override public <T extends Event<?>> void save(T event) throws Exception {
Map<String, String> data = null;
if (event instanceof ChangeEvent) {
ChangeEvent e = (ChangeEvent) event;
data = handle(e);
if (data != null) {
String uri = this.props.get("uri") + "/metrics";
HttpRequestWithBody request = Unirest.post(uri);
request.queryString("metric", e.variable());
request.queryString("value", data.get("value"));
data.remove("value");
for (String tag : data.keySet()) {
request.queryString("label[" + tag + "]", data.get(tag));
}
HttpResponse<String> response = request.asString();
if (Boolean.valueOf(this.props.get("output_responses"))) {
System.out.println(response.getStatusText() + "\t"
+ response.getBody());
}
}
}
}
示例2: login
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Use this method to get a new TelegramBot instance with the selected auth token
*
* @param authToken The bots auth token
*
* @return A new TelegramBot instance or null if something failed
*/
public static TelegramBot login(String authToken) {
try {
HttpRequestWithBody request = Unirest.post(API_URL + "bot" + authToken + "/getMe");
HttpResponse<String> response = request.asString();
JSONObject jsonResponse = Utils.processResponse(response);
if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) {
JSONObject result = jsonResponse.getJSONObject("result");
return new TelegramBot(authToken, result.getInt("id"), result.getString("first_name"), result.getString("username"));
}
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
示例3: uploadOne
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
@Test
public void uploadOne() throws IOException {
// Create temp file.
File temp = File.createTempFile("myimage", ".image");
Files.write("Hello world", temp, Charsets.UTF_8);
try {
HttpRequestWithBody post = Unirest.post("http://localhost:4849/upload/image");
post.field("name", "bingoo.txt");
post.field("file", temp);
HttpResponse<String> file = post
.asString();
System.out.println(file.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
temp.delete();
}
示例4: 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);
}
}