本文整理汇总了Java中com.mashape.unirest.http.Unirest类的典型用法代码示例。如果您正苦于以下问题:Java Unirest类的具体用法?Java Unirest怎么用?Java Unirest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Unirest类属于com.mashape.unirest.http包,在下文中一共展示了Unirest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestHttp
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
/**
* Using the HttpMethod to return a http request.
* @param params Parameters to be replaced.
* @return The http request
*/
private HttpRequest requestHttp(Object... params) {
String processedPath = processPath(params);
HttpRequest request = null;
switch (path.getMethod()) {
case GET:
request = Unirest.get(processedPath); break;
case HEAD:
request = Unirest.head(processedPath); break;
case POST:
request = Unirest.post(processedPath); break;
case PUT:
request = Unirest.put(processedPath); break;
case PATCH:
request = Unirest.patch(processedPath); break;
case DELETE:
request = Unirest.delete(processedPath); break;
case OPTIONS:
request = Unirest.options(processedPath); break;
}
processRequest(request);
this.request = request;
return request;
}
示例2: test_formParams_work
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
@Test
public void test_formParams_work() throws Exception {
Javalin app = Javalin.create().port(0).start();
app.before("/body-reader", ctx -> ctx.header("X-BEFORE", ctx.formParam("username")));
app.post("/body-reader", ctx -> ctx.result(ctx.formParam("password")));
app.after("/body-reader", ctx -> ctx.header("X-AFTER", ctx.formParam("repeat-password")));
HttpResponse<String> response = Unirest
.post("http://localhost:" + app.port() + "/body-reader")
.body("username=some-user-name&password=password&repeat-password=password")
.asString();
assertThat(response.getHeaders().getFirst("X-BEFORE"), is("some-user-name"));
assertThat(response.getBody(), is("password"));
assertThat(response.getHeaders().getFirst("X-AFTER"), is("password"));
app.stop();
}
示例3: execute
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public void execute(JobExecutionContext context) throws JobExecutionException {
// Extract data from job
Map<String, String> properties = (Map<String, String>) context.getMergedJobDataMap().get(Map.class.getCanonicalName());
Set<String> taskIds = (Set<String>) context.getMergedJobDataMap().get(Task.class.getCanonicalName());
taskIds.forEach(taskId -> {
final String url = urlConverter(
DEFAULT_PROTOCOL,
properties.get(SERVER_IP),
properties.get(SERVER_PORT),
properties.get(SERVER_CONTEXT_PATH) + RETRY_PATH + "/" + taskId
);
try {
final HttpResponse<String> httpResponse =
Unirest.get(url).asString();
} catch (UnirestException e) {
LOGGER.error("UnirestException", e);
}
});
}
示例4: retrieveAllFiles
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public static List<String> retrieveAllFiles(String auth, String folder) throws IOException, UnirestException {
List<String> lis= new LinkedList<String>();
HttpResponse<JsonNode> jsonResponse = Unirest.get("https://www.googleapis.com/drive/v2/files/root/children?q=title='"+folder+"'").header("Authorization","Bearer "+auth).asJson();
JSONObject jsonObject= new JSONObject(jsonResponse.getBody());
JSONArray array = jsonObject.getJSONArray("array");
for(int i=0;i<array.length();i++){
JSONArray jarray=array.getJSONObject(i).getJSONArray("items");
int j=jarray.length();
while(j>0){
String id=jarray.getJSONObject(0).getString("id");
auxRetrieveAllFiles(lis,auth,"https://www.googleapis.com/drive/v2/files?includeTeamDriveItems=false&pageSize=500&q='"+id+"'%20in%20parents"+"&key="+ MISConfig.getGoogle_api(),id);
j--;
}
}
return lis;
}
示例5: paste
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public static String paste(String toSend) {
try {
String pasteToken = Unirest.post("https://hastebin.com/documents")
.header("User-Agent", "Mantaro")
.header("Content-Type", "text/plain")
.body(toSend)
.asJson()
.getBody()
.getObject()
.getString("key");
return "https://hastebin.com/" + pasteToken;
} catch (UnirestException e) {
log.warn("Hastebin is being funny, huh? Cannot send or retrieve paste.", e);
return "Bot threw ``" + e.getClass().getSimpleName() + "``" + " while trying to upload paste, check logs";
}
}
示例6: triggerJob
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
private synchronized void triggerJob() {
try {
logger.log(Level.INFO,"Starting Jenkins job: " + JOB_NAME);
String result = null;
switch(getJobType()) {
case Parametrized:
result = Unirest.post(JENKINS_URL + "/job/" + JOB_NAME + "/build").basicAuth(USER, API_TOKEN)
.field("json", getJSON())
.asBinary().getStatusText();
break;
case Normal:
result = Unirest.post(JENKINS_URL + "/job/" + JOB_NAME + "/build").basicAuth(USER, API_TOKEN)
.asBinary().getStatusText();
break;
}
logger.log(Level.INFO,"Done. Job '" + JOB_NAME + "' status: " + result);
} catch (UnirestException e) {
e.printStackTrace();
}
}
示例7: logout
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
/**
* Logs out with the session identifier.
*
* @param url REST API Url.
* @param sessionId Session identifier.
*/
public static void logout(String url, String sessionId) {
try {
Map<String, String> session = new LinkedHashMap<String, String>();
session.put("user_name", sessionId);
ObjectMapper mapper = new ObjectMapper();
String jsonSessionData = mapper.writeValueAsString(session);
Map<String, Object> request = new LinkedHashMap<String, Object>();
request.put("method", "logout");
request.put("input_type", "json");
request.put("response_type", "json");
request.put("rest_data", jsonSessionData);
Unirest.post(url)
.fields(request)
.asString();
}
catch (Exception exception) {
}
}
示例8: getToken
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
@Override
public String getToken(String code) throws UnirestException {
HttpResponse<JsonNode> response = Unirest.post("https://www.googleapis.com/oauth2/v4/token")
.header("User-Agent", "pxls.space")
.field("grant_type", "authorization_code")
.field("code", code)
.field("redirect_uri", getCallbackUrl())
.field("client_id", App.getConfig().getString("oauth.google.key"))
.field("client_secret", App.getConfig().getString("oauth.google.secret"))
.asJson();
JSONObject json = response.getBody().getObject();
if (json.has("error")) {
return null;
} else {
return json.getString("access_token");
}
}
示例9: getToken
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public String getToken(String code) throws UnirestException {
HttpResponse<JsonNode> response = Unirest.post("https://oauth.vk.com/access_token")
.header("User-Agent", "pxls.space")
.field("grant_type", "authorization_code")
.field("code", code)
.field("redirect_uri", getCallbackUrl())
.field("client_id", App.getConfig().getString("oauth.vk.key"))
.field("client_secret", App.getConfig().getString("oauth.vk.secret"))
.asJson();
JSONObject json = response.getBody().getObject();
if (json.has("error")) {
return null;
} else {
return json.getString("access_token");
}
}
示例10: getIdentifier
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public String getIdentifier(String token) throws UnirestException, InvalidAccountException {
String[] codes = token.split("\\|");
HttpResponse<JsonNode> me = Unirest.get("https://api.tumblr.com/v2/user/info?" + getOauthRequest("https://api.tumblr.com/v2/user/info", "oauth_token="+codes[0], "oob", "GET", codes[1]))
.header("User-Agent", "pxls.space")
.asJson();
JSONObject json = me.getBody().getObject();
if (json.has("error")) {
return null;
} else {
try {
return json.getJSONObject("response").getJSONObject("user").getString("name");
} catch (JSONException e) {
return null;
}
}
}
示例11: getToken
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public String getToken(String code) throws UnirestException {
HttpResponse<JsonNode> response = Unirest.post("https://discordapp.com/api/oauth2/token")
.header("User-Agent", "pxls.space")
.field("grant_type", "authorization_code")
.field("code", code)
.field("redirect_uri", getCallbackUrl())
.basicAuth(App.getConfig().getString("oauth.discord.key"), App.getConfig().getString("oauth.discord.secret"))
.asJson();
JSONObject json = response.getBody().getObject();
if (json.has("error")) {
return null;
} else {
return json.getString("access_token");
}
}
示例12: getIdentifier
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public String getIdentifier(String token) throws UnirestException, InvalidAccountException {
HttpResponse<JsonNode> me = Unirest.get("https://discordapp.com/api/users/@me")
.header("Authorization", "Bearer " + token)
.header("User-Agent", "pxls.space")
.asJson();
JSONObject json = me.getBody().getObject();
if (json.has("error")) {
return null;
} else {
long id = json.getLong("id");
long signupTimeMillis = (id >> 22) + 1420070400000L;
long ageMillis = System.currentTimeMillis() - signupTimeMillis;
long minAgeMillis = App.getConfig().getDuration("oauth.discord.minAge", TimeUnit.MILLISECONDS);
if (ageMillis < minAgeMillis){
long days = minAgeMillis / 86400 / 1000;
throw new InvalidAccountException("Account too young");
}
return json.getString("id");
}
}
示例13: getToken
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public String getToken(String code) throws UnirestException {
HttpResponse<JsonNode> response = Unirest.post("https://www.reddit.com/api/v1/access_token")
.header("User-Agent", "pxls.space")
.field("grant_type", "authorization_code")
.field("code", code)
.field("redirect_uri", getCallbackUrl())
.basicAuth(App.getConfig().getString("oauth.reddit.key"), App.getConfig().getString("oauth.reddit.secret"))
.asJson();
JSONObject json = response.getBody().getObject();
if (json.has("error")) {
return null;
} else {
return json.getString("access_token");
}
}
示例14: getIdentifier
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public String getIdentifier(String token) throws UnirestException, InvalidAccountException {
HttpResponse<JsonNode> me = Unirest.get("https://oauth.reddit.com/api/v1/me")
.header("Authorization", "bearer " + token)
.header("User-Agent", "pxls.space")
.asJson();
JSONObject json = me.getBody().getObject();
if (json.has("error")) {
return null;
} else {
long accountAgeSeconds = (System.currentTimeMillis() / 1000 - json.getLong("created"));
long minAgeSeconds = App.getConfig().getDuration("oauth.reddit.minAge", TimeUnit.SECONDS);
if (accountAgeSeconds < minAgeSeconds){
long days = minAgeSeconds / 86400;
throw new InvalidAccountException("Account too young");
} else if (!json.getBoolean("has_verified_email")) {
throw new InvalidAccountException("Account must have a verified e-mail");
}
return json.getString("name");
}
}
示例15: getAllDeployments
import com.mashape.unirest.http.Unirest; //导入依赖的package包/类
public JSONArray getAllDeployments() {
JSONArray deployments = new JSONArray();
try {
deployments =
Unirest.get(MessageFormat.format("http://{0}:{1}/deployments", host, port))
.header("accept", "application/json")
.header("Content-Type", "application/json")
.asJson()
.getBody().getArray();
} catch (UnirestException e) {
e.printStackTrace();
}
return deployments;
}