java类com.mashape.unirest.http.Headers的实例源码

FacebookMessengerHandlerImpl.java 文件源码 项目:facebook-messenger-java-lib 阅读 22 收藏 0 点赞 0 评论 0
public void sendTextMessage(final String textMessage, final Long recipientId)
{
    final FacebookMessengerMessage facebookMessengerMessage = new FacebookMessengerMessage.Builder()
        .withMessage(textMessage)
        .withRecipient(recipientId)
        .build();

    try
    {
        final Headers headers = Unirest.post(this.messengerProperties.getMessageEndpoint())
            .header("Content-Type", "application/json")
            .queryString(ACCESS_TOKEN, this.messengerProperties.getPageAccessToken())
            .body(facebookMessengerMessage.toJson())
            .asJson()
            .getHeaders();

        LOGGER.debug(headers.toString());
    }
    catch (final UnirestException e)
    {
        LOGGER.error(e.toString());
    }
}
TestProxyBMPClient.java 文件源码 项目:browsermob-proxy-client 阅读 24 收藏 0 点赞 0 评论 0
@Test
public void testOverridesResponseAsResponseFilter() throws Throwable {
    Headers headersExpected = new Headers();
    List<String> accessControlAllowCredentialsList = new ArrayList<>();
    accessControlAllowCredentialsList.add("test");
    accessControlAllowCredentialsList.add("test2");
    headersExpected.put(ACCESS_CONTROL_ALLOW_CREDENTIALS, accessControlAllowCredentialsList);
    List<String> accessControlMaxAgeList = new ArrayList<>();
    accessControlMaxAgeList.add("test3");
    headersExpected.put(ACCESS_CONTROL_MAX_AGE, accessControlMaxAgeList);
    io.netty.handler.codec.http.HttpResponse responseOverrides = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.FORBIDDEN);
    for (String headers : headersExpected.keySet()) {
        for (String headersValue : headersExpected.get(headers)) {
            responseOverrides.headers().add(headers, headersValue);
        }
    }
    HttpMessageContents contents = new HttpMessageContents(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.FORBIDDEN));
    contents.setTextContents("<html><body>Response successfully intercepted</body></html>");
    BMPResponseFilter bmpResponseFilter = new BMPResponseFilter(responseOverrides, contents, null);
    getBmpLittleProxy().setFilterResponse(bmpResponseFilter);
    Unirest.setProxy(new HttpHost(getBmpLittleProxy().getAddress(), getBmpLittleProxy().getPort()));
    HttpResponse<String> response = Unirest.get(URL_PROTOCOL + URL_FOR_TEST).asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
}
Requester.java 文件源码 项目:J-Cord 阅读 21 收藏 0 点赞 0 评论 0
private void checkRateLimit(HttpResponse<JsonNode> response) {
    final Headers headers = response.getHeaders();
    if (headers.containsKey("Retry-After")) { // Rate limited
        Long retryAfter = Long.parseLong(headers.getFirst("Retry-After")); // In milliseconds
        LOG.log(LogLevel.ERROR, "You are being rate limited! Automatically blocked the thread.\n" +
                "(Request: "+path.toString()+" | Retry after: "+retryAfter+" ms)");
        try {
            Thread.sleep(retryAfter);
        } catch (InterruptedException e) {
            LOG.log(LogLevel.ERROR, "Error when blocking thread for rate limit: ", null);
            e.printStackTrace();
        }
    }
}
TestProxyBMPClient.java 文件源码 项目:browsermob-proxy-client 阅读 22 收藏 0 点赞 0 评论 0
@Test
public void testOverridesResponseAsResponseFilterAndType() throws Throwable {
    Headers headersExpected = new Headers();
    List<String> accessControlAllowCredentialsList = new ArrayList<>();
    accessControlAllowCredentialsList.add("test");
    accessControlAllowCredentialsList.add("test2");
    headersExpected.put(ACCESS_CONTROL_ALLOW_CREDENTIALS, accessControlAllowCredentialsList);
    List<String> accessControlMaxAgeList = new ArrayList<>();
    accessControlMaxAgeList.add("test3");
    headersExpected.put(ACCESS_CONTROL_MAX_AGE, accessControlMaxAgeList);
    io.netty.handler.codec.http.HttpResponse responseOverrides = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.FORBIDDEN);
    for (String headers : headersExpected.keySet()) {
        for (String headersValue : headersExpected.get(headers)) {
            responseOverrides.headers().add(headers, headersValue);
        }
    }
    HttpMessageContents contents = new HttpMessageContents(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.FORBIDDEN));
    contents.setTextContents("<html><body>Response successfully intercepted</body></html>");
    List<FilterUrls> filterUrls = new ArrayList<>();
    DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
    httpHeaders.add(HttpHeaders.Names.CONTENT_TYPE, "text/css");
    filterUrls.add(new FilterUrls("^http:\\/\\/search\\.maven\\.org\\/(.*)$", HttpMethod.GET, httpHeaders));

    BMPResponseFilter bmpResponseFilter = new BMPResponseFilter(responseOverrides, contents, null, filterUrls);
    getBmpLittleProxy().setFilterResponse(bmpResponseFilter);
    Unirest.setProxy(new HttpHost(getBmpLittleProxy().getAddress(), getBmpLittleProxy().getPort()));

    HttpResponse<String> response = Unirest.get("http://search.maven.org/ajaxsolr/css/central.css").asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);

    response = Unirest.get("http://search.maven.org/test.html").asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);

    response = Unirest.get("http://search.maven.org/").asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
}
GitHubAPIHttpClient.java 文件源码 项目:kafka-connect-github-source 阅读 29 收藏 0 点赞 0 评论 0
protected JSONArray getNextIssues(Integer page, Instant since) throws InterruptedException {

        HttpResponse<JsonNode> jsonResponse;
        try {
            jsonResponse = getNextIssuesAPI(page, since);

            // deal with headers in any case
            Headers headers = jsonResponse.getHeaders();
            XRateLimit = Integer.valueOf(headers.getFirst("X-RateLimit-Limit"));
            XRateRemaining = Integer.valueOf(headers.getFirst("X-RateLimit-Remaining"));
            XRateReset = Integer.valueOf(headers.getFirst("X-RateLimit-Reset"));
            switch (jsonResponse.getStatus()){
                case 200:
                    return jsonResponse.getBody().getArray();
                case 401:
                    throw new ConnectException("Bad GitHub credentials provided, please edit your config");
                case 403:
                    // we have issues too many requests.
                    log.info(jsonResponse.getBody().getObject().getString("message"));
                    log.info(String.format("Your rate limit is %s", XRateLimit));
                    log.info(String.format("Your remaining calls is %s", XRateRemaining));
                    log.info(String.format("The limit will reset at %s",
                            LocalDateTime.ofInstant(Instant.ofEpochSecond(XRateReset), ZoneOffset.systemDefault())));
                    long sleepTime = XRateReset - Instant.now().getEpochSecond();
                    log.info(String.format("Sleeping for %s seconds", sleepTime ));
                    Thread.sleep(1000 * sleepTime);
                    return getNextIssues(page, since);
                default:
                    log.error(constructUrl(page, since));
                    log.error(String.valueOf(jsonResponse.getStatus()));
                    log.error(jsonResponse.getBody().toString());
                    log.error(jsonResponse.getHeaders().toString());
                    log.error("Unknown error: Sleeping 5 seconds " +
                            "before re-trying");
                    Thread.sleep(5000L);
                    return getNextIssues(page, since);
            }
        } catch (UnirestException e) {
            e.printStackTrace();
            Thread.sleep(5000L);
            return new JSONArray();
        }
    }
TestProxyBMPClient.java 文件源码 项目:browsermob-proxy-client 阅读 23 收藏 0 点赞 0 评论 0
@Test
public void testOverridesResponseAsResponseFilterAndListUrl() throws Throwable {
    Headers headersExpected = new Headers();
    List<String> accessControlAllowCredentialsList = new ArrayList<>();
    accessControlAllowCredentialsList.add("test");
    accessControlAllowCredentialsList.add("test2");
    headersExpected.put(ACCESS_CONTROL_ALLOW_CREDENTIALS, accessControlAllowCredentialsList);
    List<String> accessControlMaxAgeList = new ArrayList<>();
    accessControlMaxAgeList.add("test3");
    headersExpected.put(ACCESS_CONTROL_MAX_AGE, accessControlMaxAgeList);
    io.netty.handler.codec.http.HttpResponse responseOverrides = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.FORBIDDEN);
    for (String headers : headersExpected.keySet()) {
        for (String headersValue : headersExpected.get(headers)) {
            responseOverrides.headers().add(headers, headersValue);
        }
    }
    HttpMessageContents contents = new HttpMessageContents(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.FORBIDDEN));
    contents.setTextContents("<html><body>Response successfully intercepted</body></html>");
    List<FilterUrls> filterUrls = new ArrayList<>();
    filterUrls.add(new FilterUrls("(.*)index\\.html(.*)"));
    filterUrls.add(new FilterUrls("^http:\\/\\/search\\.maven\\.org\\/$", HttpMethod.GET));
    filterUrls.add(new FilterUrls("(.*)test\\.html(.*)", HttpMethod.POST));
    BMPResponseFilter bmpResponseFilter = new BMPResponseFilter(responseOverrides, contents, null, filterUrls);
    getBmpLittleProxy().setFilterResponse(bmpResponseFilter);
    Unirest.setProxy(new HttpHost(getBmpLittleProxy().getAddress(), getBmpLittleProxy().getPort()));

    HttpResponse<String> response = Unirest.get(URL_PROTOCOL + URL_FOR_TEST).asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
    response = Unirest.post(URL_PROTOCOL + URL_FOR_TEST).asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);

    response = Unirest.get("http://search.maven.org/index.html").asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
    response = Unirest.post("http://search.maven.org/index.html").asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);

    response = Unirest.get("http://search.maven.org/test.html").asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
    response = Unirest.post("http://search.maven.org/test.html").asString();
    assertOverrideResponseEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);

    response = Unirest.get("http://search.maven.org/abracadabra.alibaba").asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
    response = Unirest.post("http://search.maven.org/abracadabra.alibaba").asString();
    assertOverrideResponseNotEquals(accessControlAllowCredentialsList, accessControlMaxAgeList, response);
}


问题


面经


文章

微信
公众号

扫码关注公众号