jodd.http.HttpRequest.get()方法的使用及代码示例

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

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

HttpRequest.get介绍

[英]Builds a GET request.
[中]生成GET请求。

代码示例

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

@Test
void testBigRequest() throws IOException {
  InputStream inputStream = ClassLoaderUtil.getResourceAsStream("/jodd/http/answer.json");
  FastCharArrayWriter writter = StreamUtil.copy(inputStream);
  String body = writter.toString();
  HttpRequest httpRequest = HttpRequest.get("").body(body);
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  httpRequest.sendTo(outputStream);
  String receivedBody = outputStream.toString();
  int ndx = receivedBody.indexOf("{");
  receivedBody = receivedBody.substring(ndx);
  assertEquals(body, receivedBody);
}

代码示例来源: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 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 testTimeout() {
    HttpRequest httpRequest = HttpRequest.get("localhost:8173/slow");
    httpRequest.timeout(1000);

    try {
      httpRequest.send();
      fail("error");
    }
    catch(HttpException ignore) {
    }

    httpRequest = HttpRequest.get("localhost:8173/slow");
    httpRequest.timeout(6000);

    int status = httpRequest.send().statusCode();

    assertEquals(200, status);
  }
}

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

@Test
void testNegativeContentLength() {
  HttpRequest request = HttpRequest.get("http://jodd.org/?id=173");
  request.contentLength(-123);
  byte[] bytes = request.toByteArray();
  try {
    HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes));
    assertEquals("", request2.body());
  } catch (Exception ex) {
    fail(ex.toString());
  }
  // the same test but with missing content length
  request = HttpRequest.get("http://jodd.org/?id=173");
  bytes = request.toByteArray();
  try {
    HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes));
    assertEquals("", request2.body());
  } catch (Exception ex) {
    fail(ex.toString());
  }
}

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

@Test
  void testHttpRequestContentSetOrder() {
    final HttpRequest request = HttpRequest.get("http://127.0.0.1:8086/test");

    request.contentTypeJson().bodyText("{}");

    assertEquals(MimeTypes.MIME_APPLICATION_JSON, request.mediaType());
  }
}

代码示例来源: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 testDefaultParametersOverwrite() {
    HttpBrowser httpBrowser = new HttpBrowser();
    httpBrowser.setDefaultHeader("aaa", "123");

    HttpRequest request = HttpRequest.get("foo.com");
    request.header("aaa", "987");

    httpBrowser.addDefaultHeaders(request);

    assertEquals(2, request.headerNames().size());
    assertEquals("987", request.header("aaa"));
  }
}

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

@Test
void testDirect() {
  HttpResponse response = HttpRequest.get("http://localhost:1080/get_books").send();
  assertEquals(200, response.statusCode());
  assertTrue(response.body().contains("Tatum"));
  proxy.verify(request().withPath("/get_books"), exactly(0));
}

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

@Test
void testDirectHttps() {
  HttpResponse response = HttpRequest.get("https://localhost:1080/get_books").trustAllCerts(true).send();
  assertEquals(200, response.statusCode());
  assertTrue(response.body().contains("Tatum"));
  proxy.verify(request().withPath("/get_books"), exactly(0));
}

代码示例来源: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: oblac/jodd

@Test
void testEcho() throws IOException {
  EchoTestServer echoTestServer = new EchoTestServer();
  HttpResponse response = HttpRequest.get("http://localhost:8081/hello?id=12").send();
  assertEquals(200, response.statusCode());
  assertEquals("OK", response.statusPhrase());
  assertEquals("GET", echoTestServer.method);
  assertEquals("/hello", echoTestServer.uri);
  assertEquals(1, echoTestServer.params.size());
  assertEquals("12", echoTestServer.params.get("id"));
  assertEquals("GET /hello", response.body());
  echoTestServer.stop();
}

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

@Test
void testDefaultParameters() {
  HttpBrowser httpBrowser = new HttpBrowser();
  httpBrowser.setDefaultHeader("aaa", "123");
  HttpRequest request = HttpRequest.get("foo.com");
  request.header("bbb", "987");
  httpBrowser.addDefaultHeaders(request);
  assertEquals(3, request.headerNames().size());
  assertEquals("123", request.header("aaa"));
  assertEquals("987", request.header("bbb"));
}

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

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

代码示例来源: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 testSocks5Proxy() {
  SocketHttpConnectionProvider s = new SocketHttpConnectionProvider();
  s.useProxy(ProxyInfo.socks5Proxy("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"));
  proxy.verify(request().withPath("/get_books"), exactly(1));
}

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

@Test
void testSocks5ProxyWithHttps() {
  SocketHttpConnectionProvider s = new SocketHttpConnectionProvider();
  s.useProxy(ProxyInfo.socks5Proxy("localhost", 1090, null, null));
  HttpResponse response = HttpRequest.get("https://localhost:1080/get_books")
    .withConnectionProvider(s)
    .trustAllCerts(true)
    .send();
  assertEquals(200, response.statusCode());
  assertTrue(response.body().contains("Tatum"));
  proxy.verify(request().withPath("/get_books"), exactly(1));
}

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

@Test
void testBrowser() {
  HttpBrowser httpBrowser = new HttpBrowser();
  httpBrowser.sendRequest(
    HttpRequest
      .get("localhost:8173/echo?id=17")
      .cookies(new Cookie("waffle", "jam"))
      .bodyText("hello"));
  HttpResponse httpResponse = httpBrowser.getHttpResponse();
  assertNotNull(httpResponse);
  assertEquals("hello", httpResponse.body());
  Cookie[] cookies = httpResponse.cookies();
  assertEquals(1, cookies.length);
  assertEquals("waffle", cookies[0].getName());
  assertEquals("jam!", cookies[0].getValue());
}

代码示例来源: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 testContentTypeHeader() {
  HttpRequest req = HttpRequest.get("localhost/hello");
  assertNull(req.contentType());
  req.contentType("text/plain;charset=UTF-8");
  assertEquals("text/plain", req.mediaType());
  assertEquals("UTF-8", req.charset());
  assertEquals("text/plain;charset=UTF-8", req.contentType());
  req.mediaType("text/html");
  assertEquals("text/html;charset=UTF-8", req.contentType());
  req.mediaType(null);
  assertEquals("text/html;charset=UTF-8", req.contentType());
  req.charset("ASCII");
  assertEquals("text/html;charset=ASCII", req.contentType());
  req.charset(null);
  assertEquals("text/html", req.contentType());
  req.contentType("text/plain;charset=UTF-8;boundary=123");
  assertEquals("text/plain", req.mediaType());
  assertEquals("UTF-8", req.charset());
  assertEquals("text/plain;charset=UTF-8;boundary=123", req.contentType());
}

相关文章