本文整理了Java中jodd.http.HttpRequest.post()
方法的一些代码示例,展示了HttpRequest.post()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.post()
方法的具体详情如下:
包路径:jodd.http.HttpRequest
类名称:HttpRequest
方法名:post
[英]Builds a POST request.
[中]生成POST请求。
代码示例来源:origin: oblac/jodd
@Test
void testHttpRequestSlash() {
HttpRequest request = HttpRequest.post("/");
request.contentType("application/x-www-form-urlencoded");
HttpRequest request1 = HttpRequest.readFrom(new ByteArrayInputStream(request.toByteArray()));
assertEquals(request.toString(), request1.toString());
}
代码示例来源: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 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
@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 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
(i == 1 || i == 2) ?
HttpRequest.get("http://localhost:8173/echo?id=12"):
HttpRequest.post("http://localhost:8173/echo?id=12");
代码示例来源: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: 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 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: binarywang/WxJava
@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: com.github.binarywang/weixin-java-mp
@Override
public Boolean 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);
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent, WxType.MP);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}
}
}
代码示例来源:origin: binarywang/WxJava
@Override
public Boolean 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);
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent, WxType.MP);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}
}
}
代码示例来源:origin: com.github.binarywang/weixin-java-mp
@Override
public WxMpMaterialVideoInfoResult 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);
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent, WxType.MP);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.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: binarywang/WxJava
@Override
public WxMpMaterialVideoInfoResult 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);
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent, WxType.MP);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.fromJson(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: 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);
}
}
内容来源于网络,如有侵权,请联系作者删除!