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

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

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

HttpRequest.send介绍

[英]#open() if not already open, sends request, reads response and closes the request. If keep-alive mode is enabled connection will not be closed.
[中]#open()如果尚未打开,则发送请求、读取响应并关闭请求。如果启用保持活动模式,则不会关闭连接。

代码示例

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

/**
 * Syntax sugar.
 */
public void sendAndReceive(final Consumer<HttpResponse> responseHandler) {
  responseHandler.accept(send());
}

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

/**
 * Syntax sugar.
 */
public <R> R sendAndReceive(final Function<HttpResponse, R> responseHandler) {
  return responseHandler.apply(send());
}

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

@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

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 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"));
}

代码示例来源: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
@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 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
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

@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 testUploadWithUploadable() throws IOException {
  EchoTestServer echoTestServer = new EchoTestServer();
  HttpResponse response = HttpRequest
      .post("http://localhost:8081/hello")
      .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("POST", echoTestServer.method);
  assertEquals("12", echoTestServer.params.get("id"));
  File uploadedFile = new File(echoTestServer.files.get("file").toString());
  assertNotNull(uploadedFile);
  assertEquals("upload тест", FileUtil.readString(uploadedFile));
  assertEquals("POST /hello", response.body());
  echoTestServer.stop();
}

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

@Test
void testUpload() throws IOException {
  EchoTestServer echoTestServer = new EchoTestServer();
  File file = FileUtil.createTempFile();
  file.deleteOnExit();
  FileUtil.writeString(file, "upload тест");
  assertEquals("upload тест", FileUtil.readString(file));
  HttpResponse response = HttpRequest
      .post("http://localhost:8081/hello")
      .form("id", "12")
      .form("file", file)
      .send();
  assertEquals(200, response.statusCode());
  assertEquals("OK", response.statusPhrase());
  assertEquals("POST", echoTestServer.method);
  assertEquals("12", echoTestServer.params.get("id"));
  File uploadedFile = new File(echoTestServer.files.get("file").toString());
  assertNotNull(uploadedFile);
  assertEquals("upload тест", FileUtil.readString(uploadedFile));
  assertEquals("POST /hello", response.body());
  echoTestServer.stop();
  file.delete();
}

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

@Test
void testUploadWithMonitor() throws IOException {
  EchoTestServer echoTestServer = new EchoTestServer();
  File file = FileUtil.createTempFile();
  file.deleteOnExit();
  FileUtil.writeString(file, StringUtil.repeat('A', 1024));
  final StringBuilder sb = new StringBuilder();
  HttpResponse response = HttpRequest
      .post("http://localhost:8081/hello")
      .form("id", "12")
      .form("file", file)
      .monitor(new HttpProgressListener() {
        @Override
        public void transferred(int len) {
          sb.append(":" + len);
        }
      })
      .send();
  assertEquals(200, response.statusCode());
  assertEquals("OK", response.statusPhrase());
  echoTestServer.stop();
  file.delete();
  assertEquals(":0:512:1024:148", StringUtil.substring(sb.toString(), 0, -1));
}

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

assertArrayEquals(utf8Bytes, request.bodyBytes());
HttpResponse response = request.send();
assertEquals(200, response.statusCode());

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

private void testFormParams(int i) {
  String encoding = i == 1 ?  "UTF-8" : "CP1251";
  HttpRequest request = HttpRequest.post("http://localhost:8173/echo3");
  request.formEncoding(encoding);
  if (i == 3) {
    request.charset("UTF-8");
  }
  String value1 = "value";
  String value2 = "валуе";
  request.form("one", value1);
  request.form("two", value2);
  if (i != 3) {
    request.form("enc", encoding);
  }
  HttpResponse httpResponse = request.send();
  assertEquals("application/x-www-form-urlencoded", request.mediaType());
  if (i == 3) {
    assertEquals("UTF-8", request.charset());
    assertEquals("CP1251", request.formEncoding);
  } else {
    assertNull(request.charset());
  }
  assertFalse(Data.ref.get);
  assertTrue(Data.ref.post);
  assertEquals(i == 3 ? 2 : 3, Data.ref.params.size());
  assertEquals(value1, Data.ref.params.get("one"));
  assertEquals(value2, Data.ref.params.get("two"));
}

相关文章