QT学习05:qt客户端访问java环境服务端

一、java服务端代码:

package example;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 * @Author CHQ
 * @Date 2022/6/30 10:19
 */@WebService()
public class HelloWorld {
  @WebMethod
  public String sayHelloWorldFrom(String from) {
    String result = "Hello, world, from " + from;
    System.out.println(result);
    return result;
  }

  @WebMethod
  public String getValue(String name) {
    System.out.println("客户端访问者: "+ name);
    return "客户端访问者: "+ name;
  }

  public static void main(String[] argv) {
    Object implementor = new HelloWorld ();
    String address = "http://localhost:9000/HelloWorld";
    Endpoint.publish(address, implementor);
    System.out.println("服务端成功启动!");
  }
}

二、Qt客户端主要步骤:

1)下载gsoap2.8版本备用。

2)用gsoap工具里gsoap\bin\win64\下的wsdl2h.exe操作"url?wsdl"(进入wsdl2h.exe所在目录的cmd下执行下面),得到包含所有服务端方法的头文件(自己命名为mySoap.h)。

wsdl2h -o mySoap.h http://localhost:9000/HelloWorld?wsdl

3)用gsoap工具里gsoap\bin\win64\下的soapcpp2.exe操作mySoap.h即可得到一系列相关的文件,新生成的都剪切到新建文件夹mygsoap内;再将gsoap下的stdsoap2.h、stdsoap2.cpp复制到mygsoap。

soapcpp2.exe mySoap.h

3)新建Qt客户端项目TheClient,将mygsoap放这个项目文件下面。在.pro文件下引入上文生成的所需文件( soapC.cpp、soapClient.cpp、stdsoap2.cpp)。

QT -= gui

CONFIG += c++11

TARGET = NewClient
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += \
        main.cpp \
        mygsoap/soapC.cpp \
       mygsoap/soapClient.cpp \
        mygsoap/stdsoap2.cpp \

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

INCLUDEPATH +=./mygsoap

#解决部分编译问题
LIBS += D:\Qt\Qt5.9.0\Tools\mingw530_32\i686-w64-mingw32\lib\libws2_32.a

4)在main.cpp调用服务端暴露出来的接口方法(这里调用getValue())。

#include <QDebug>
#include "soapH.h"  // include the generated source code headers
#include "HelloWorldPortBinding.nsmap" // include XML namespaces
#include<string>

using namespace std;

int main(int argc, char *argv[])
{
    struct soap _soap;
    soap_init(&_soap);
    soap_set_mode(&_soap,SOAP_C_UTFSTRING);  //解决gsoap中文乱码问题
    // return =""; //SOAP返回值,若不为0则表示调用出错
    QString errStr = ""; //出错提示
    QString strRes = ""; //结果字符串

    //request和response
    ns1__getValue req;
    ns1__getValueResponse res;

    //填充参数
    string a="chq";
    req.arg0=&a;
    const char *point=NULL;
    const char *ction=NULL;

    //调用SOAP   /*soap_call___ns1__接口**/
    if(soap_call___ns1__getValue(&_soap,point,ction, &req, res)== SOAP_OK){

        qDebug()<<"访问成功!";
    }

    if(_soap.error != 0){
        //获取错误提示
        errStr = QString::fromLocal8Bit(*soap_faultstring(&_soap));
    }
    else {
        //获取结果字符串
        strRes = res.return_->c_str();
        qDebug()<<strRes;
    }
    soap_end(&_soap);
    soap_done(&_soap);

    return 0;
}

5)结果

 参考博客:

(1)

QT环境下Webservice服务调用,服务器端是在Java环境下_小夹子351的博客-CSDN博客

(2) 

Qt使用gSoap实现webservice服务端和客户端_hitzsf的博客-CSDN博客_qt webservice

(3)  解决问题:

QT5 gSoap WebService 调用天气预报_happy_tom的博客-CSDN博客

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值