vertx post与get写法

复制代码
            vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false)).request(HttpMethod.POST, "45.xxx.127.130", "/gogo", resp -> {
                                                                   ip或域名     域名或ip后的路劲
                resp.bodyHandler(buf -> {
                    JsonObject jsonObject = new JsonObject(buf.toString());
                    System.out.println(jsonObject);
                    System.out.println(jsonObject.getString("msg"));
                });
            }).exceptionHandler(ex -> {
                ex.printStackTrace();
                msg.fail(0, "请求额外失败");
            }).putHeader("Content-Type","application/x-www-form-urlencoded").end("signMethod="+signMethod+"&signature="+signature+"&"+for_post);
复制代码

POST方式和GET的区别就是把发送的数据和网页地址分离开来。把数据放在HTTP协议的head部分,因此程序中多了一个 设置请求头 ("Content-Type: application/x-www-form-urlencoded") 这个内容你还可以根据你的需要来改变,例如改为图像,或者二进制文件等。

 

一、Form表单语法
在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。 例如: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,这个一般文件上传时用。 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
补充

二、常用的编码方式
form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。

1.x-www-form-urlencoded
当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url。

2.multipart/form-data
当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。

 

引用自 : https://blog.csdn.net/qq_28702545/article/details/51719199

 

 

 

-------------------------------------------------------------------------------------------------------------------

 

 

 get写法1

复制代码
            String for_get = "mchNo=" + mchNo + "&" +
                    "notifyUrl=" + notifyUrl + "&" +
                    "orderCode=" + orderCode + "&" +
                    "price=" + price + "&" +
                    "ts=" + ts + "&" +
                    "type=" + type + "&"+
                    "sign=" + sign;

            vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false)).request(HttpMethod.GET, "www.xxxx.com", "/api/getQrcode?"+for_get, resp -> {
                resp.bodyHandler(buf -> {
                    if (params.getString("payWaySan_jhzf").equals("JHZF_zfb")) {

                        System.out.println(buf.toString());

                        JsonObject json = new JsonObject(buf.toString());

                        msg.reply(json.getString("payUrl"));
                    }
                });
            }).exceptionHandler(ex -> {
                ex.printStackTrace();
                msg.fail(0, "请求额外失败");
            }).end();
复制代码

 get写法2

复制代码
            String for_get = "p00_spid="+p00_spid+"&"+
                    "p01_paytype="+p01_paytype+"&"+
                    "p02_spbillno="+p02_spbillno+"&"+
                    "p03_money="+p03_money+"&"+
                    "p04_callbackurl="+p04_callbackurl+"&"+
                    "p05_returnurl="+p05_returnurl+"&"+
                    "p07_productname="+p07_productname+"&"+
                    "pn_sign="+sign;

            System.out.println(for_get);

            vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false)).request(HttpMethod.GET,2188,"47.52.209.6", "/tongda/v6/pay", resp -> {
                resp.bodyHandler(buf -> {
                    System.out.println(resp.statusCode());
                    System.out.println("返回 ===== :"+buf.toString());

//                    JsonObject json = new JsonObject(buf.toString());
//
//                    if(json.getInteger("status") == 200){
//                        System.out.println(new JsonObject(buf.toString()).toString());
//                        msg.reply(json.getString("payUrl"));
//                    }else {
//                        msg.fail(0, "失败");
//                    }

                });
            }).exceptionHandler(ex -> {
                ex.printStackTrace();
                msg.fail(0, "请求TongDa失败");
            }).putHeader("Content-Type","application/x-www-form-urlencoded").end(for_get);
复制代码

 

posted on   --LP--  阅读(2170)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示