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

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

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

HttpResponse介绍

[英]HTTP response.
[中]HTTP响应。

代码示例

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

HttpResponse response = HttpResponse.readFrom(new ByteArrayInputStream(fileContent.getBytes("UTF-8")));
assertEquals("102", headers.get("content-length"));
assertEquals("no-cache", response.header("Pragma"));
assertEquals("text/html;charset=UTF-8" , response.contentType());
assertEquals("text/html" , response.mediaType());
assertEquals("UTF-8" , response.charset());
assertNotNull(response.contentLength());
String rawBody = response.body();
String textBody = response.bodyText();

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

@Test
void testRawResponse6() throws IOException {
  URL data = RawTest.class.getResource("6-response.txt");
  String fileContent = FileUtil.readString(data.getFile());
  fileContent = StringUtil.replace(fileContent, "\n", "\r\n");
  fileContent = StringUtil.replace(fileContent, "\r\r\n", "\r\n");
  HttpResponse response = HttpResponse.readFrom(new ByteArrayInputStream(fileContent.getBytes("UTF-8")));
  assertEquals(200, response.statusCode());
  assertEquals("", response.statusPhrase);
  String body = response.bodyText();
  assertEquals(
      "Wikipedia in\n" +
      "\n" +
      "chunks.", body.replace("\r\n", "\n"));
  assertEquals("TheData", response.header("SomeAfterHeader"));
}

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

/**
 * Parses 'location' header to return the next location or returns {@code null} if location not specified.
 * Specification (<a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30">rfc2616</a>)
 * says that only absolute path must be provided, however, this does not
 * happens in the real world. There a <a href="https://tools.ietf.org/html/rfc7231#section-7.1.2">proposal</a>
 * that allows server name etc to be omitted.
 */
public String location() {
  String location = header("location");
  if (location == null) {
    return null;
  }
  if (location.startsWith(StringPool.SLASH)) {
    location = getHttpRequest().hostUrl() + location;
  }
  return location;
}

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

/**
 * Unzips GZip-ed body content, removes the content-encoding header
 * and sets the new content-length value.
 */
public HttpResponse unzip() {
  String contentEncoding = contentEncoding();
  if (contentEncoding != null && contentEncoding().equals("gzip")) {
    if (body != null) {
      headerRemove(HEADER_CONTENT_ENCODING);
      try {
        ByteArrayInputStream in = new ByteArrayInputStream(body.getBytes(StringPool.ISO_8859_1));
        GZIPInputStream gzipInputStream = new GZIPInputStream(in);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamUtil.copy(gzipInputStream, out);
        body(out.toString(StringPool.ISO_8859_1));
      } catch (IOException ioex) {
        throw new HttpException(ioex);
      }
    }
  }
  return this;
}

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

HttpResponse httpResponse = new HttpResponse();
    httpResponse.httpVersion(line.substring(0, ndx));
    httpResponse.httpVersion(HTTP_1_1);
    ndx2 = -1;
    ndx = 0;
    httpResponse.statusCode(Integer.parseInt(line.substring(ndx, ndx2).trim()));
    httpResponse.statusCode(-1);
  httpResponse.statusPhrase(line.substring(ndx2).trim());
httpResponse.readHeaders(reader);
httpResponse.readBody(reader);

代码示例来源:origin: com.gitee.morilys.jsmile/jsmile-kit

/**
 * http get请求
 * @param url 请求地址
 * @param param 请求参数
 * @return
 */
public static String post(String url, Map<String,Object> param,Map<String,String> headers){
  String result=null;
  try {
    HttpRequest httpRequest = HttpRequest.post(url);
    if(null!=param){
      httpRequest.form(param);
    }
    if(headers!=null&&!headers.isEmpty()){
      httpRequest.header(headers);
    }
    HttpResponse response = httpRequest.send().charset(HttpConstant.Charsets.DEFAULT_CHARSET);
    result = response.bodyText();
  }catch (Exception e){
    logger.error("HTTP请求地址【" + url + "】发生异常:" + ExceptionUtils.getRootCause(e));
  }
  return result;
}

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

@Override
 public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
  if (ticket != null) {
   if (uri.indexOf('?') == -1) {
    uri += '?';
   }
   uri += uri.endsWith("?")
    ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
    : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
  }

  HttpRequest request = HttpRequest.get(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String contentTypeHeader = response.header("Content-Type");
  if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
   String responseContent = response.bodyText();
   throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
  }
  try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
   return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
  }
 }
}

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

assertEquals(200, response.statusCode());
assertEquals(String.valueOf(utf8StringRealLen), response.contentLength());
assertEquals("text/html;charset=UTF-8", response.contentType());
assertEquals(utf8String, response.bodyText());
assertEquals(new String(utf8Bytes, StringPool.ISO_8859_1), response.body());

代码示例来源: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 testRawResponse5() throws IOException {
  URL data = RawTest.class.getResource("5-response.txt");
  String fileContent = FileUtil.readString(data.getFile());
  fileContent = StringUtil.replace(fileContent, "\n", "\r\n");
  fileContent = StringUtil.replace(fileContent, "\r\r\n", "\r\n");
  HttpResponse response = HttpResponse.readFrom(new ByteArrayInputStream(fileContent.getBytes("UTF-8")));
  String body = response.bodyText();
  assertEquals(
      "Wikipedia in\n" +
      "\n" +
      "chunks.", body.replace("\r\n", "\n"));
  assertEquals("TheData", response.header("SomeAfterHeader"));
}

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

clientResponse.status(response.statusCode(), response.statusPhrase());
clientResponse.body(response.bodyText());
response.headers().forEach(
  header -> clientResponse.headers().add(
    header.getKey(), header.getValue()));

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

httpResponse = new HttpResponse();
    httpResponse.assignHttpRequest(httpRequest);
    httpResponse.statusCode(503);
    httpResponse.statusPhrase("Service unavailable. " + ExceptionUtil.message(httpException));
int statusCode = httpResponse.statusCode();
  String newPath = httpResponse.location();
  String newPath = httpResponse.location();
  String newPath = httpResponse.location();

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

private String getResponseString(HttpResponse response) throws WxPayException {
 try {
  this.log.debug("【微信服务器响应头信息】:\n{}", response.toString(false));
 } catch (NullPointerException e) {
  this.log.warn("HttpResponse.toString() 居然抛出空指针异常了", e);
 }
 String responseString = response.bodyText();
 if (StringUtils.isBlank(responseString)) {
  throw new WxPayException("响应信息为空");
 }
 if (StringUtils.isBlank(response.charset())) {
  responseString = new String(responseString.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
 }
 return responseString;
}

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

@Test
void testNoContentLength() throws IOException {
  URL data = RawTest.class.getResource("3-response.txt");
  byte[] fileContent = FileUtil.readBytes(data.getFile());
  HttpResponse httpResponse = HttpResponse.readFrom(new ByteArrayInputStream(fileContent));
  assertEquals("Body!", httpResponse.bodyText());
}

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

/**
 * Returns last response HTML page.
 */
public String getPage() {
  if (httpResponse == null) {
    return null;
  }
  return httpResponse.bodyText();
}

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

HttpResponse response = HttpResponse.readFrom(in);
if (response.body() != null) {
  response.headerRemove("Transfer-Encoding");
  response.contentLength(response.body().length());
response.sendTo(socketOutput);

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

clientResponse.statusCode(response.statusCode());
clientResponse.statusMessage(response.statusPhrase());
clientResponse.body(response.body());
Map<String, String[]> responseHeaders = response.headers();

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

相关文章