com.github.kevinsawicki.http.HttpRequest类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(275)

本文整理了Java中com.github.kevinsawicki.http.HttpRequest类的一些代码示例,展示了HttpRequest类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest类的具体详情如下:
包路径:com.github.kevinsawicki.http.HttpRequest
类名称:HttpRequest

HttpRequest介绍

[英]A fluid interface for making HTTP requests using an underlying HttpURLConnection (or sub-class).

Each instance supports making a single request and cannot be reused for further requests.
[中]使用底层HttpURLConnection(或子类)发出HTTP请求的流体接口。
每个实例都支持发出单个请求,并且不能再用于其他请求。

代码示例

代码示例来源:origin: com.restfuse/com.eclipsesource.restfuse

public ResponseImpl( HttpRequest request ) {
 body = request.body();
 contentType = request.contentType();
 headers = request.headers();
 code = request.code();
 url = request.getConnection().getURL().toString();
 request.disconnect();
}

代码示例来源:origin: lkorth/httpebble-android

/**
 * Start a 'POST' request to the given URL along with the query params
 *
 * @param baseUrl
 * @param params  the query parameters to include as part of the baseUrl
 * @param encode  true to encode the full URL
 * @return request
 * @see #append(CharSequence, Map)
 * @see #encode(CharSequence)
 */
public static HttpRequest post(final CharSequence baseUrl,
                final Map<?, ?> params, final boolean encode) {
  String url = append(baseUrl, params);
  return post(encode ? encode(url) : url);
}

代码示例来源:origin: lkorth/httpebble-android

/**
 * Start a 'GET' request to the given URL along with the query params
 *
 * @param baseUrl
 * @param params  The query parameters to include as part of the baseUrl
 * @param encode  true to encode the full URL
 * @return request
 * @see #append(CharSequence, Map)
 * @see #encode(CharSequence)
 */
public static HttpRequest get(final CharSequence baseUrl,
               final Map<?, ?> params, final boolean encode) {
  String url = append(baseUrl, params);
  return get(encode ? encode(url) : url);
}

代码示例来源:origin: com.github.kevinsawicki/http-request

/**
 * Get response as {@link String} using character set returned from
 * {@link #charset()}
 *
 * @return string
 * @throws HttpRequestException
 */
public String body() throws HttpRequestException {
 return body(charset());
}

代码示例来源:origin: com.github.kevinsawicki/http-request

/**
 * Get parameter with given name from header value in response
 *
 * @param headerName
 * @param paramName
 * @return parameter value or null if missing
 */
public String parameter(final String headerName, final String paramName) {
 return getParam(header(headerName), paramName);
}

代码示例来源:origin: com.github.kevinsawicki/http-request

/**
 * Get all parameters from header value in response
 * <p>
 * This will be all key=value pairs after the first ';' that are separated by
 * a ';'
 *
 * @param headerName
 * @return non-null but possibly empty map of parameter headers
 */
public Map<String, String> parameters(final String headerName) {
 return getParams(header(headerName));
}

代码示例来源:origin: ihaolin/antares

private String doPost() {
  HttpRequest post = HttpRequest.post(url, params, encode)
      .headers(headers)
      .connectTimeout(connectTimeout)
      .readTimeout(readTimeout)
      .acceptGzipEncoding()
      .uncompress(true);
  setOptionalHeaders(post);
  if (!Strings.isNullOrEmpty(body)){
    post.send(body);
  }
  if (ssl){
    trustHttps(post);
  }
  return post.body();
}

代码示例来源:origin: ihaolin/antares

private String doGet() {
  HttpRequest get = HttpRequest.get(url, params, encode)
      .headers(headers)
      .connectTimeout(connectTimeout)
      .readTimeout(readTimeout)
      .acceptGzipEncoding()
      .uncompress(true);
  if (ssl){
    trustHttps(get);
  }
  setOptionalHeaders(get);
  return get.body();
}

代码示例来源:origin: restx/restx

/**
 * Share the stats to share URL. Must not be called if sharing is disabled.
 * shareEnabled check is not done to avoid double checking.
 */
private void shareStats() {
  try {
    int code = HttpRequest.post(shareURL)
        .connectTimeout(5000)
        .readTimeout(5000)
        .send(objectMapper.writer().writeValueAsString(stats).getBytes(Charsets.UTF_8))
        .code();
    if (code >= 400) {
      logger.info("sharing stats on {} failed. Response code: {}", shareURL, code);
    }
  } catch (Exception e) {
    logger.info("sharing stats on {} failed. Exception: {}", shareURL, e.getMessage());
  }
}

代码示例来源:origin: ihaolin/antares

/**
   * download a file
   * @param url http url
   * @param output the output which downloaded content will fill into
   */
  public static void download(String url, OutputStream output){
    try {
      HttpRequest request =  HttpRequest.get(url);
      if (request.ok()){
        request.receive(output);
      } else {
        throw new HttpException("request isn't ok: " + request.body());
      }
    } catch (Exception e){
      throw new HttpException(e);
    }
  }
}

代码示例来源:origin: stackoverflow.com

HttpRequest req = HttpRequest.get("https://google.com");
req.trustAllCerts();
req.trustAllHosts(); //If you are having certificate problems
int code = req.code();
String body = req.body();
Log.d("CODE:", String.valueOf(code));
Log.d("BODY:", body);

代码示例来源:origin: SINTEF-9012/cloudml

/**
 * This method sends a request to upload a monitoring rule to the monitoring manager.
 @param rule is the monitoring rule to be uploaded
 @return the response of the monitoring manager
 */
public String addMonitoringRule(String rule){
  String url = address + "/" + version + "/monitoring-rules";
  String response = null;
  try {
    response = HttpRequest.post(url).send(rule).body();
  } catch (Exception e) {
    journal.log(Level.INFO, "Connection to the monitoring manager refused");
  }
  return response;
}

代码示例来源:origin: com.codeslap/github-jobs-java-api

public static boolean subscribe(String email, String description, String location, boolean fullTime) {
  HashMap<String, String> parameters = new HashMap<String, String>();
  parameters.put(SUBSCRIPTION_EMAIL_PARAM, email);
  parameters.put(SUBSCRIPTION_DESCRIPTION_PARAM, description);
  parameters.put(SUBSCRIPTION_LOCATION_PARAM, location);
  parameters.put(SUBSCRIPTION_FULL_TIME_PARAM, String.valueOf(fullTime));
  String response = HttpRequest.post(ApiConstants.EMAIL_SUBSCRIPTION_URL)
      .part(SUBSCRIPTION_EMAIL_PARAM, email)
      .part(SUBSCRIPTION_DESCRIPTION_PARAM, description)
      .part(SUBSCRIPTION_LOCATION_PARAM, location)
      .part(SUBSCRIPTION_FULL_TIME_PARAM, String.valueOf(fullTime))
      .body();
  return SUBSCRIPTION_OK_PARAM.equals(response);
}

代码示例来源:origin: SINTEF-9012/cloudml

/**
 * This method sends a request to attach an observer to a specific metric
 * @param callback the address on which the observer is running
 *
 * @param metric the requested metric
 */
public void attachObserver(String callback, String metric) {
  String url = address + "/" + version + "/metrics/" + metric + "/observers";
  try {
  HttpRequest.post(url).send(callback).code();
      journal.log(Level.INFO, "Observer attached");
  } catch (Exception e) {
    journal.log(Level.INFO, "Connection to the monitoring manager refused");
  }
}

代码示例来源:origin: com.codeslap/github-jobs-java-api

public User getUser(String username) {
    String url = String.format(ApiConstants.API_URL, String.format(ApiConstants.GET_USER, username));
    try {
      String response = HttpRequest.get(url).body();
      // convert json to object
      Gson gson = new Gson();
      return gson.fromJson(response, User.class);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}

代码示例来源:origin: stackoverflow.com

HttpRequest request = HttpRequest.post(url);
 request.authorization("Basic "+ah);
 request.part("file", fName+".png", "image/png", new File(file));
 request.part("title", "test");
 if(request.code()==201) {
  StringWriter sw = new StringWriter();
  request.receive(sw);
  onMedia(Media.parse(new  JsonParser().parse(sw.toString()).getAsJsonObject()));
 }

代码示例来源:origin: k55k32/cms-admin-end

public String getToken(String code) throws JsonProcessingException, IOException {
  String body = HttpRequest.post(ACCESS_TOKEN_URL,
      ImmutableMap.of(
          "client_id", config.getGithubClientId(),
          "client_secret", config.getGithubClientSecret(),
          "code", code
          ),
      false).header("Accept", "application/json").body();
  JsonNode node = om.readTree(body);
  if (node.has("access_token")) {
    String token = node.get("access_token").asText();
    return token;
  } else {
    throw new AppException(Error.AUTH2_CODE_ERROR, body);
  }
}

代码示例来源:origin: com.github.kevinsawicki/http-request

/**
 * Write part of a multipart request to the request body
 *
 * @param name
 * @param part
 * @return this request
 */
public HttpRequest part(final String name, final String part) {
 return part(name, null, part);
}

代码示例来源:origin: no.cantara.base/Hystrix-BaseCommands

@Override
  protected HttpRequest dealWithRequestBeforeSend(HttpRequest request) {
    super.dealWithRequestBeforeSend(request);
//        request.getConnection().addRequestProperty("SOAPAction", SOAP_ACTION);
    String query = buildSoapXml();
    request.contentType("text/xml;charset=UTF-8").send(query);
    return request;
  }

代码示例来源:origin: com.github.kevinsawicki/http-request

/**
 * Is the response code a 200 OK?
 *
 * @return true if 200, false otherwise
 * @throws HttpRequestException
 */
public boolean ok() throws HttpRequestException {
 return HTTP_OK == code();
}

相关文章

HttpRequest类方法