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

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

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

HttpRequest.form介绍

暂无

代码示例

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

@Test
void testFormParamsObjects() {
  Map<String, Object> params = new HashMap<>();
  params.put("state", 1);
  HttpRequest httpRequest = new HttpRequest();
  httpRequest.form(params);
  assertEquals(1, httpRequest.form().size());
}

代码示例来源: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 testHttpRequestReRead() {
  HttpRequest request = HttpRequest.post("http://127.0.0.1:8086/test");
  request.form("a", null);
  request.form("b", "aaa");
  HttpRequest request1 = HttpRequest.readFrom(new ByteArrayInputStream(request.toByteArray()));
  assertEquals(request.toString(), request1.toString());
}

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

代码示例来源: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 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 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 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 testFileUpload() throws IOException {
  HttpRequest request = HttpRequest.get("http://jodd.org/?id=173");
  request.header("User-Agent", "Scaly").form("one", "funny");
  File tempFile = FileUtil.createTempFile();
  tempFile.deleteOnExit();
  FileUtil.writeString(tempFile, "qwerty");
  request.form("two", tempFile);
  byte[] bytes = request.toByteArray();
  // read
  HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes));
  HttpMultiMap<?> httpParams2 = request2.form();
  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"));
  FileUpload fu = (FileUpload) httpParams2.get("two");
  assertEquals(6, fu.getSize());
  String str = new String(fu.getFileContent());
  assertEquals("qwerty", str);
  tempFile.delete();
}

代码示例来源: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: 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.liferay.launchpad/api-client

param -> httpRequest.form(param.getKey(), param.getValue()));

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

throw new FileNotFoundException();
request.form("media", file);
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
 request.form("description", WxGsonBuilder.create().toJson(form));

代码示例来源:origin: binarywang/WxJava

throw new FileNotFoundException();
request.form("media", file);
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
 request.form("description", WxGsonBuilder.create().toJson(form));

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

@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: com.github.binarywang/weixin-java-mp

@Override
 public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException {
  if (data == null) {
   throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件对象为空").build());
  }

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

  request.form("media", data);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  }

  return WxMediaImgUploadResult.fromJson(responseContent);
 }
}

代码示例来源:origin: binarywang/WxJava

@Override
 public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException {
  if (data == null) {
   throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件对象为空").build());
  }

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

  request.form("media", data);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  }

  return WxMediaImgUploadResult.fromJson(responseContent);
 }
}

相关文章