主题评价:
  • 0 次(票) - 平均星级: 0
  • 1
  • 2
  • 3
  • 4
  • 5
使用HttpGet发送formdata格式数据
#1
今天对接第三方接口,遇到个奇怪的方式,要求使用HttpGet发送FormData,但是吧使用HttpGet发送FormData格式的数据不是标准做法,因为HttpGet通常用于获取资源,它的设计不包括请求体(request body)。当然人家既然弄出这样的接口,就表示一般是可以实现的,
以下是实现源码
  • HttpGetWithBody继承HttpEntityEnclosingRequestBase ,而HttpEntityEnclosingRequestBase支持携带body
代码:
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

import java.net.URI;

/**
* httpget带body
*
* @author 1dream.fun
* @since 2024/10/15 14:54
*/
public class HttpGetWithBody extends HttpEntityEnclosingRequestBase {
    public static final String METHOD_NAME = "GET";
    HttpGetWithBody(final String uri) {
        super();
        setURI(URI.create(uri));
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }
}
  • 接口实现,具体以实际需求进行改动
代码:
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
    /**
     * 获取form请求结果
     * @param url 地址
     * @param headers 请求头
     * @param body 请求form参数
     * @date 2024/10/18 11:08
     * @return java.lang.String
     */
    private static String getWithForm(String url, Map<String,String> headers,Map<String,Object> body) throws IOException {
        HttpGetWithBody httpGet = new HttpGetWithBody(url);
        headers.forEach(httpGet::setHeader);
        final MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        body.forEach((k,v)-> multipartEntityBuilder.addTextBody(k, Optional.ofNullable(v).map(Object::toString).orElse("")));
        httpGet.setEntity(multipartEntityBuilder.build());
        return EntityUtils.toString(HttpClients.createDefault().execute(httpGet).getEntity());
    }
以上就是本次实现 使用HttpGet发送formdata格式数据的解决办法
Cool他什么都没有说,却好像什么都说了
回复


这个主题的帖子
使用HttpGet发送formdata格式数据 - by wangxg - 10-18-2024, 12:22
Test, just a test - by Michaelkew - 01-19-2025, 00:48
Tulip. Soil. Vilnius. - by atollxeyExcuri - 02-10-2025, 13:39
ygvlqhnx rdusm - by usonlinepbj - 02-19-2025, 09:13
Лечение акне - by Lechenie_yhoi - 03-22-2025, 09:16
fast indexing api - by MichaelMat - 06-01-2025, 19:02
Awr434365476# - by JamesDep - 06-06-2025, 18:58
boll send solid - by crctransport - 06-10-2025, 13:08
boll send purchase - by JamesPhist - 06-13-2025, 05:51
directory jaxx wallet - by Charlesasype - 06-11-2025, 19:14
zDTvHFu2zgx34ZNBn6H1tema - by StevenNeest - 01-31-2025, 20:07
boll send secure - by JosephJed - 06-03-2025, 06:29
auto news toyota, honda etc. - by Ronaldproma - 05-27-2025, 18:39
dig this jaxx wallet - by DanielNit - 06-01-2025, 02:56
fast google indexing - by MichaelMat - 05-28-2025, 18:00
boll send achievement - by Haroldmen - 06-10-2025, 05:43
boll send get hold of - by crctransport - 06-10-2025, 10:24
boll send solid - by BrandonAlosy - 05-30-2025, 19:35
boll send get - by GarrygautS - 06-09-2025, 20:54
Dw. Mike shanahan. Cum. - by atollxeyExcuri - 02-09-2025, 23:24
казино 7к Я 92 - by kscasinmerce - 05-21-2025, 14:55
boll send achievement - by StevenNep - 06-08-2025, 22:21
boll send take - by crctransport - 06-10-2025, 12:22
Seeking insight - by BasilBut - 06-09-2025, 00:37
why not find out more - by Dextermet - 06-13-2025, 14:52
Ботокс фулфейс - by Botoks_glei - 03-05-2025, 04:17
botox - by botox_aksr - 03-06-2025, 06:08
Лечение акне - by Lechenie_egoi - 04-16-2025, 19:47
Curated Film Selections - by AlfredGaile - 04-25-2025, 06:01
4543sert435545kjhkhj44 - by Harrywib - 04-29-2025, 15:17
FFssdaebve436456 - by MichaelMat - 05-25-2025, 19:13
Источник - by MichaelNer - 05-31-2025, 02:15
Darkweb Polska - by Mariodeese - 昨天, 13:05
This Site - by Charlesemulp - 06-11-2025, 21:37
BdMjcc4b6jjujkpMoSoU - by GreggMum - 01-29-2025, 17:14
4543sert435545kjhkhj44 - by AnthonyMat - 04-29-2025, 18:21
4543sert435545kjhkhj44 - by AnthonyMat - 05-10-2025, 04:52
кент казино Ю 66 - by kentcGok - 05-28-2025, 17:34
boll send harvest - by WilliamDroth - 05-30-2025, 17:21
boll send solid - by Donnietix - 06-04-2025, 18:22
boll send take it - by RichardGer - 06-04-2025, 18:45
fast indexing api - by Belovzorovsab - 06-01-2025, 08:34
BwSMk00UoUk4kWoqQpUA - by FrankEvisa - 02-05-2025, 17:57
BwSMk00UoUk4kWoqQpUA - by Robertdog - 02-06-2025, 22:32
Discover 10-Film Lists - by AlfredGaile - 04-28-2025, 16:03
4543sert435545kjhkhj44 - by Terrywib - 05-10-2025, 01:43
speedyindex google forms - by MichaelMat - 05-28-2025, 14:17
boll send hold - by Matthewworge - 05-30-2025, 20:53
boll send achievement - by LarryNed - 05-30-2025, 21:22
boll send secure - by Raymondces - 06-03-2025, 12:12
boll send hold - by Davidacify - 06-04-2025, 04:09
этот контент - by Roberthed - 06-10-2025, 16:10
boll send assumed - by RobertDit - 06-09-2025, 21:13
boll send secure - by Williamsnigo - 昨天, 12:07
Homepage jaxx wallet liberty - by Perryapemy - 06-10-2025, 15:52
Ботокс фулфейс - by Botoks_fzei - 02-25-2025, 06:58
Monica - by Monica keype - 04-26-2025, 08:14
learn this here now - by Anthonysmuch - 05-31-2025, 03:47
RE: ??HttpGet??formdata???? - by Masterpkx - 02-22-2025, 09:09
Лечение акне - by Lechenie_jgoi - 02-24-2025, 09:28
RE: ??HttpGet??formdata???? - by Ninjabis - 03-06-2025, 13:45
boll send buy - by HarleyBus - 05-28-2025, 11:07
boll send harvest - by Alfredhievy - 05-30-2025, 05:23
GENOCIDE of Miriam Adelson - by NormandFrome - 06-05-2025, 03:42
boll send believe - by RobertWes - 06-04-2025, 04:01
boll send take - by Donnietix - 06-04-2025, 19:03
boll send purchase - by Haroldmen - 06-10-2025, 05:47
boll send solid - by GarrettAwaks - 06-05-2025, 03:35
boll send gain - by DavidFus - 06-07-2025, 20:03
boll send take it - by DavidFus - 06-07-2025, 20:05
RE: ??HttpGet??formdata???? - by Samvdl - 03-26-2025, 05:00
kraken сайт - by Josephknock - 06-07-2025, 17:08
RE: ??HttpGet??formdata???? - by Wifeane - 03-26-2025, 07:18
Ботокс - by Botoks_coPt - 04-07-2025, 23:22
RE: ??HttpGet??formdata???? - by Cativp - 03-26-2025, 09:46
speed index how to fix - by Belovzorovsab - 05-28-2025, 06:26
boll send believe - by Donaldsek - 05-31-2025, 03:56
boll send harvest - by PatrickRon - 06-01-2025, 05:08
boll send get - by RobertMut - 06-07-2025, 06:25
boll send hold - by Jesusgom - 06-11-2025, 03:54
RE: ??HttpGet??formdata???? - by Wolfuaq - 03-26-2025, 11:47
RE: ??HttpGet??formdata???? - by Goodvck - 03-26-2025, 17:20
fast indexing - by Shraunjzt - 06-02-2025, 01:16
boll send get - by KevinPeX - 06-08-2025, 20:10
RE: ??HttpGet??formdata???? - by Cyberyly - 03-26-2025, 19:50
RE: ??HttpGet??formdata???? - by Ironmzx - 03-31-2025, 06:17
RE: ??HttpGet??formdata???? - by Masterxui - 03-31-2025, 11:43
RE: ??HttpGet??formdata???? - by Iceclm - 04-09-2025, 07:54
RE: ??HttpGet??formdata???? - by Peopledvq - 04-09-2025, 09:45
4543sert435545kjhkhj44 - by Harrywib - 04-29-2025, 16:30
RE: ??HttpGet??formdata???? - by Starreo - 05-04-2025, 18:11
RE: ??HttpGet??formdata???? - by Davidrip - 05-06-2025, 16:15
RE: ??HttpGet??formdata???? - by Russellgot - 05-08-2025, 04:41
RE: ??HttpGet??formdata???? - by JosephMow - 05-08-2025, 11:00
RE: ??HttpGet??formdata???? - by MichaelTrose - 05-09-2025, 05:51
RE: ??HttpGet??formdata???? - by Thomasblurb - 05-09-2025, 06:34
RE: ??HttpGet??formdata???? - by MichaelTrose - 05-10-2025, 00:01
RE: ??HttpGet??formdata???? - by MichaelTrose - 05-10-2025, 04:52
RE: ??HttpGet??formdata???? - by EugeneAlith - 05-10-2025, 07:09
RE: ??HttpGet??formdata???? - by MichaelTrose - 05-10-2025, 17:12
RE: ??HttpGet??formdata???? - by AntonTipsy - 05-10-2025, 18:28
RE: ??HttpGet??formdata???? - by EugeneAlith - 05-10-2025, 19:16
RE: ??HttpGet??formdata???? - by AaronZet - 05-11-2025, 12:58
RE: ??HttpGet??formdata???? - by MichaelTrose - 05-11-2025, 16:10
RE: ??HttpGet??formdata???? - by MichaelTrose - 05-11-2025, 21:53
RE: ??HttpGet??formdata???? - by MichaelTrose - 05-12-2025, 02:14
may I ask? - by AlexeyBum - 05-18-2025, 09:01
RE: ??HttpGet??formdata???? - by OlgaDaw - 05-12-2025, 16:57
RE: ??HttpGet??formdata???? - by CarlosCab - 05-12-2025, 23:39
RE: ??HttpGet??formdata???? - by Jamesanide - 05-13-2025, 03:02
RE: ??HttpGet??formdata???? - by Norberttop - 05-13-2025, 20:44
RE: ??HttpGet??formdata???? - by MichaelTrose - 05-14-2025, 01:41
RE: ??HttpGet??formdata???? - by Jessebrurl - 05-14-2025, 07:49
RE: ??HttpGet??formdata???? - by LolaDaw - 05-15-2025, 23:34
RE: ??HttpGet??formdata???? - by RichardTes - 05-17-2025, 04:32
boll send secure - by Williamsnigo - 昨天, 12:29
RE: ??HttpGet??formdata???? - by Lloydbus - 05-19-2025, 10:34
0day music - by BufordTew - 05-23-2025, 08:12
KOA FAIRLAUNCH - by Jessieslupe - 05-28-2025, 19:37
boll send get - by Terrelllex - 05-30-2025, 17:26
boll send gain - by RobertDow - 06-01-2025, 03:31
boll send get hold of - by Robertken - 06-04-2025, 04:05
boll send secure - by GarrygautS - 06-09-2025, 21:00
boll send believe - by RichardRaike - 06-12-2025, 07:03
boll send gain - by JamesPhist - 06-13-2025, 05:12
RE: ??HttpGet??formdata???? - by ElisDaw - 05-20-2025, 03:36
RE: ??HttpGet??formdata???? - by ElisDaw - 05-20-2025, 07:12
RE: ??HttpGet??formdata???? - by GregoryFrase - 05-20-2025, 16:18
boll send payout - by Millardmoils - 06-13-2025, 11:25
her latest blog - by WilliamRoona - 06-03-2025, 12:25
fast indexing windows - by Belovzorovsab - 05-29-2025, 16:29
boll send payout - by Errolwaync - 06-07-2025, 01:19
boll send assumed - by Shermandrofe - 06-07-2025, 16:00
boll send get - by Larryrig - 06-07-2025, 20:05
boll send harvest - by Danielflons - 06-08-2025, 02:58
boll send payout - by GeraldLat - 06-08-2025, 07:59
boll send secure - by Williamsnigo - 昨天, 12:05
fast indexing - by Shraunjzt - 05-28-2025, 10:47
boll send get - by CurtisDes - 06-04-2025, 04:03
boll send assumed - by CurtisDes - 06-04-2025, 11:33
boll send purchase - by CurtisDes - 06-04-2025, 04:05
boll send take - by SidneyNor - 06-07-2025, 05:39
aprender - by BobbySloxy - 昨天, 15:35
boll send achievement - by VirgilChisk - 06-08-2025, 07:58
boll send secure - by KevinPeX - 06-08-2025, 20:09
boll send payout - by Millardmoils - 06-13-2025, 11:00
boll send assumed - by WilliamLoorm - 11 小时 之前
fast indexing of links using - by Shraunjzt - 05-29-2025, 01:41
boll send assumed - by RichardCam - 06-02-2025, 15:00
boll send assume trust to - by Davidacify - 06-04-2025, 03:31
boll send purchase - by Robertken - 06-04-2025, 04:06
boll send hold - by GarrettAwaks - 06-05-2025, 08:30
boll send payout - by ThomasWew - 06-07-2025, 02:06
boll send solid - by GarrygautS - 06-09-2025, 20:56
SpeedyIndex google - by Shraunjzt - 05-29-2025, 10:17
RE: ??HttpGet??formdata???? - by GregoryFrase - 05-30-2025, 10:17
fast indexing aamir iqbal - by Shraunjzt - 06-01-2025, 12:29
boll send believe - by RichardRaike - 06-12-2025, 07:44
boll send secure - by Rickygerie - 06-07-2025, 04:27
my site toast wallet app - by Walterfap - 06-10-2025, 12:01
boll send purchase - by JamesPhist - 06-13-2025, 05:18
vbucks купить - by Frankfluor - 06-03-2025, 16:29
boll send purchase - by Jesusgom - 06-11-2025, 05:57
Continued Metamask Extension - by DiegoLoort - 06-05-2025, 23:58
RE: ??HttpGet??formdata???? - by GregoryFrase - 06-07-2025, 15:36
boll send gain - by GeraldLat - 06-08-2025, 08:03
kraken онион тор - by WilliamNaf - 06-08-2025, 13:39
boll send believe - by Haroldmen - 06-10-2025, 06:11
boll send secure - by GarrygautS - 06-09-2025, 21:25
boll send assumed - by RobertDit - 06-09-2025, 21:40
boll send take - by Jesusgom - 06-11-2025, 06:46
boll send harvest - by WilliamLoorm - 10 小时 之前
RE: ??HttpGet??formdata???? - by Jerrygrews - 06-11-2025, 23:04
RE: ??HttpGet??formdata???? - by Jerrygrews - 06-12-2025, 05:52
RE: ??HttpGet??formdata???? - by Jerrygrews - 06-12-2025, 11:46
RE: ??HttpGet??formdata???? - by Jerrygrews - 06-12-2025, 17:41
RE: ??HttpGet??formdata???? - by GregoryFrase - 06-12-2025, 20:37
RE: ??HttpGet??formdata???? - by Jerrygrews - 06-12-2025, 23:42
RE: ??HttpGet??formdata???? - by Jerrygrews - 06-13-2025, 05:41

论坛跳转:


正在浏览该主题的用户: arijuhu, ewedurawex, ibajpipene, ozenudayub, 1 个游客