jodd.http.HttpRequest类的使用及代码示例

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

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

HttpRequest介绍

[英]HTTP request.
[中]HTTP请求。

代码示例

代码示例来源:origin: oblac/jodd

@Test
void test394() {
  HttpRequest request = HttpRequest.get("https://jodd.org/random link");
  assertEquals("GET", request.method());
  assertEquals("https://jodd.org/random link", request.url());
  request = HttpRequest.get("https://jodd.org/random link?q=1");
  assertEquals("1", request.query().get("q"));
  String badUrl = "httpsjodd.org/random link?q=1:// GET";
  try {				
    HttpRequest.get(badUrl).send();
    fail("error");
  }
  catch (HttpException he) {
    assertTrue(he.getMessage().contains(badUrl));
  }
}

代码示例来源:origin: oblac/jodd

@Test
void testInOutForm() {
  HttpRequest request = HttpRequest.get("http://jodd.org/?id=173");
  request.header("User-Agent", "Scaly");
  request.form("one", "funny");
  byte[] bytes = request.toByteArray();
  // read
  HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes));
  assertEquals(request.method(), request2.method());
  assertEquals(request.path(), request2.path());
  assertEquals(request.queryString(), request2.queryString());
  assertEquals(request.header("User-Agent"), request2.header("User-Agent"));
  assertEquals(request.header("Content-Type"), request2.header("content-type"));
  assertEquals(request.header("Content-Length"), request2.header("content-length"));
  HttpMultiMap<?> params1 = request.form();
  HttpMultiMap<?> params2 = request2.form();
  assertEquals(params1.size(), params2.size());
  assertEquals(params2.get("one"), params2.get("one"));
}

代码示例来源:origin: oblac/jodd

@Test
void testMultipart() {
  HttpRequest request = HttpRequest.post("http://localhost:8173/echo2");
  request
    .formEncoding("UTF-8")		// optional
    .multipart(true);
  String value1 = "value";
  String value2 = "валуе";
  request.form("one", value1);
  request.form("two", value2);
  HttpResponse httpResponse = request.send();
  assertEquals("multipart/form-data", request.mediaType());
  assertFalse(Data.ref.get);
  assertTrue(Data.ref.post);
  assertEquals(value1, Data.ref.parts.get("one"));
  assertEquals(value2, Data.ref.parts.get("two"));
}

代码示例来源:origin: oblac/jodd

/**
 * Builds a DELETE request.
 */
public static HttpRequest delete(final String destination) {
  return new HttpRequest()
      .method(HttpMethod.DELETE)
      .set(destination);
}
/**

代码示例来源:origin: oblac/jodd

/**
 * Opens connection and sends a response.
 */
protected HttpResponse _sendRequest(final HttpRequest httpRequest, final HttpResponse previouseResponse) {
  if (!keepAlive) {
    httpRequest.open(httpConnectionProvider);
  } else {
    // keeping alive
    if (previouseResponse == null) {
      httpRequest.open(httpConnectionProvider).connectionKeepAlive(true);
    } else {
      httpRequest.keepAlive(previouseResponse, true);
    }
  }
  return httpRequest.send();
}

代码示例来源:origin: oblac/jodd

public static HttpRequest readFrom(final InputStream in, final String encoding) {
  BufferedReader reader;
  try {
    reader = new BufferedReader(new InputStreamReader(in, encoding));
  } catch (UnsupportedEncodingException uneex) {
    return null;
  }
  final HttpRequest httpRequest = new HttpRequest();
  httpRequest.headers.clear();
  final String line;
  try {
    line = reader.readLine();
  } catch (IOException ioex) {
    throw new HttpException(ioex);
  }
  if (!StringUtil.isBlank(line)) {
    String[] s = StringUtil.splitc(line, ' ');
    httpRequest.method(s[0]);
    httpRequest.path(s[1]);
    httpRequest.httpVersion(s[2]);
    httpRequest.readHeaders(reader);
    httpRequest.readBody(reader);
  }
  return httpRequest;
}

代码示例来源:origin: oblac/jodd

@Test
@Disabled
void testHttpProxy() {
  SocketHttpConnectionProvider s = new SocketHttpConnectionProvider();
  s.useProxy(ProxyInfo.httpProxy("localhost", 1090, null, null));
  HttpResponse response = HttpRequest.get("http://localhost:1080/get_books")
    .withConnectionProvider(s)
    .send();
  assertEquals(200, response.statusCode());
  assertTrue(response.body().contains("Tatum"));
}

代码示例来源:origin: com.github.binarywang/weixin-java-mp

@Override
 public InputStream execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.query("media_id", materialId);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
   // 下载媒体文件出错
   byte[] responseContent = IOUtils.toByteArray(inputStream);
   String responseContentString = new String(responseContent, StandardCharsets.UTF_8);
   if (responseContentString.length() < 100) {
    try {
     WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
     if (wxError.getErrorCode() != 0) {
      throw new WxErrorException(wxError);
     }
    } catch (com.google.gson.JsonSyntaxException ex) {
     return new ByteArrayInputStream(responseContent);
    }
   }
   return new ByteArrayInputStream(responseContent);
  }
 }
}

代码示例来源:origin: com.github.binarywang/weixin-java-common

@Override
 public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());
  request.form("media", file);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  }
  return WxMediaUploadResult.fromJson(responseContent);
 }
}

代码示例来源:origin: oblac/jodd

HttpRequest request = HttpRequest.get("http://jodd.org");
assertEquals("Close", request.header("Connection"));
request.connectionKeepAlive(true);
assertTrue(request.isConnectionPersistent());
HttpResponse response = request.open(httpConnectionProvider).send();
HttpConnection connection = request.connection();
assertTrue(request.isConnectionPersistent());
assertTrue(response.isConnectionPersistent());
assertNotNull(request.connection());
request = HttpRequest.get("http://jodd.org");
response = request.keepAlive(response, true).send();
assertSame(connection, request.connection());
assertTrue(request.isConnectionPersistent());
assertTrue(response.isConnectionPersistent());
assertNotNull(request.connection());
request = HttpRequest.get("http://jodd.org");
response = request.keepAlive(response, true).send();
assertNull(request.connection()); // connection is closed
assertTrue(request.isConnectionPersistent());
assertFalse(response.isConnectionPersistent());
request = HttpRequest.get("http://jodd.org");
response = request.keepAlive(response, true).send();		// should be false for the last connection, but ok.

代码示例来源:origin: com.liferay.launchpad/api-client

String url = request.url();
final HttpRequest httpRequest = new HttpRequest()
        .method(request.method())
        .set(url);
  .collect(Collectors.toList());
httpRequest.cookies(
  cookies.toArray(new jodd.http.Cookie[cookies.size()]));
  header -> httpRequest.header(header.getKey(), header.getValue()));
  param -> httpRequest.query(param.getKey(), param.getValue()));
  param -> httpRequest.form(param.getKey(), param.getValue()));
  httpRequest.bodyText(
    body, contentType.contentType(), contentType.charset());

代码示例来源:origin: oblac/jodd

@Test
void testSet() {
  HttpRequest httpRequest = new HttpRequest();
  httpRequest.set("GET http://jodd.org:173/index.html?light=true");
  assertEquals("GET", httpRequest.method());
  assertEquals("http", httpRequest.protocol());
  assertEquals("jodd.org", httpRequest.host());
  assertEquals(173, httpRequest.port());
  assertEquals("/index.html", httpRequest.path());
  assertEquals("true", httpRequest.query().get("light"));
  httpRequest = new HttpRequest();
  httpRequest.set("http://jodd.org:173/index.html?light=true");
  assertEquals("GET", httpRequest.method());
  assertEquals("http", httpRequest.protocol());
  assertEquals("jodd.org", httpRequest.host());
  assertEquals(173, httpRequest.port());
  assertEquals("/index.html", httpRequest.path());
  assertEquals("true", httpRequest.query().get("light"));
  httpRequest = new HttpRequest();
  httpRequest.set("jodd.org:173/index.html?light=true");
  assertEquals("GET", httpRequest.method());
  assertEquals("http", httpRequest.protocol());
  assertEquals("jodd.org", httpRequest.host());
  assertEquals(173, httpRequest.port());
  assertEquals("/index.html", httpRequest.path());

代码示例来源:origin: com.github.binarywang/weixin-java-mp

@Override
 public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }

  HttpRequest request = HttpRequest.post(uri)
   .withConnectionProvider(requestHttp.getRequestHttpClient())
   .body(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  this.logger.debug("响应原始数据:{}", responseContent);
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  } else {
   return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
  }
 }
}

代码示例来源:origin: oblac/jodd

private void testRequestEncoding(int i) throws IOException {
  HttpRequest request =
      (i == 1 || i == 2) ?
      HttpRequest.get("http://localhost:8173/echo?id=12"):
      HttpRequest.post("http://localhost:8173/echo?id=12");
  int utf8StringRealLen = utf8Bytes.length;
  request.bodyText(utf8String);
  String rawBody = request.body();
  assertEquals(utf8StringRealLen, rawBody.length());
  assertArrayEquals(utf8Bytes, request.bodyBytes());
  HttpResponse response = request.send();
  assertEquals(200, response.statusCode());

代码示例来源:origin: com.github.binarywang/weixin-java-pay

private HttpRequest buildHttpRequest(String url, String requestStr, boolean useKey) throws WxPayException {
 HttpRequest request = HttpRequest
  .post(url)
  .timeout(this.getConfig().getHttpTimeout())
  .connectionTimeout(this.getConfig().getHttpConnectionTimeout())
  .bodyText(requestStr);
 if (useKey) {
  SSLContext sslContext = this.getConfig().getSslContext();
  if (null == sslContext) {
   sslContext = this.getConfig().initSSLContext();
  }
  final SSLSocketHttpConnectionProvider provider = new SSLSocketHttpConnectionProvider(sslContext);
  request.withConnectionProvider(provider);
 }
 if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) {
  ProxyInfo httpProxy = new ProxyInfo(ProxyType.HTTP, this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort(),
   this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword());
  HttpConnectionProvider provider = request.connectionProvider();
  if (null == provider) {
   provider = new SocketHttpConnectionProvider();
  }
  provider.useProxy(httpProxy);
  request.withConnectionProvider(provider);
 }
 return request;
}

代码示例来源:origin: com.liferay.launchpad/api-transport-jodd

String url = request.url();
final HttpRequest httpRequest = new HttpRequest()
        .method(request.method())
        .set(url);
  httpRequest.header("Cookie",
    new jodd.http.Cookie(cookie.encode()).toString());
  httpRequest.header(
    entry.getKey(), ValuesUtil.toString(entry.getValue()));
  httpRequest.query(
    entry.getKey(), ValuesUtil.toString(entry.getValue()));
  httpRequest.body(body);

代码示例来源:origin: oblac/jodd

private void testQueryParams(int i) throws IOException {
  String encoding = i == 1 ?  "UTF-8" : "CP1251";
  HttpRequest request = HttpRequest.get("http://localhost:8173/echo2");
  request.queryEncoding(encoding);
  String value1 = "value";
  String value2 = "валуе";
  request.query("one", value1);
  request.query("two", value2);
  request.query("enc", encoding);
  HttpResponse httpResponse = request.send();
  assertTrue(Data.ref.get);
  assertFalse(Data.ref.post);
  assertEquals(3, Data.ref.params.size());
  assertEquals(value1, Data.ref.params.get("one"));
  assertEquals(value2, Data.ref.params.get("two"));
}

代码示例来源:origin: oblac/jodd

@Test
void testUrl() {
  HttpRequest httpRequest = new HttpRequest();
  httpRequest.set("GET http://jodd.org:173/index.html?light=true");
  assertEquals("http://jodd.org:173/index.html?light=true", httpRequest.url());
  assertEquals("http://jodd.org:173", httpRequest.hostUrl());
  httpRequest = HttpRequest.get("foo.com/");
  assertEquals("http://foo.com", httpRequest.hostUrl());
}

代码示例来源:origin: oblac/jodd

@Test
void testRedirect() {
  HttpRequest httpRequest = HttpRequest.get("localhost:8173/redirect");
  HttpResponse httpResponse = httpRequest.send();
  assertEquals(302, httpResponse.statusCode);
  HttpBrowser httpBrowser = new HttpBrowser();
  httpBrowser.sendRequest(
      HttpRequest.get("localhost:8173/redirect"));
  httpResponse = httpBrowser.getHttpResponse();
  assertNotNull(httpResponse);
  assertEquals("target!", httpResponse.body());
}

代码示例来源:origin: oblac/jodd

@Test
void testUploadWithUploadable() throws IOException {
  HttpResponse response = HttpRequest
      .post("http://localhost:8173/echo2")
      .multipart(true)
      .form("id", "12")
      .form("file", new ByteArrayUploadable(
        "upload тест".getBytes(StringPool.UTF_8), "d ст", MimeTypes.MIME_TEXT_PLAIN))
      .send();
  assertEquals(200, response.statusCode());
  assertEquals("OK", response.statusPhrase());
  assertEquals("12", Data.ref.params.get("id"));
  assertEquals("upload тест", Data.ref.parts.get("file"));
  assertEquals("d ст", Data.ref.fileNames.get("file"));
}

相关文章