gsoap for Qt (windows),gsoapqt

來源:互聯網
上載者:User

gsoap for Qt (windows),gsoapqt

本文經過自己實踐總結,同時參考網上另外一哥們文檔.

環境:win32,vs2008,Qt 4.7,gsoap2.7.17

一,安裝vs2008+Qt 4.7+外掛程式就不說了,直接用QtCreator也可以。

二,到官網下載gsoap2.7.17,下不到自己想辦法,我上傳群共用了。但群號不方便說。

三,源碼解壓,然後把gsoap-2.7/gsoap/bin/win32設定到系統的Path中去,這個目錄下有兩個可執行檔wsdl2h.exe和soapcpp2.exe,這兩個檔案可以根據wsdl介面檔案產生你需要的用戶端介面檔案。

四,建立伺服器webserivce測試,下面代碼拿來注意:

開啟VS2008,建立WebService工程。全部採用預設設定就可以了。

最後修改一下代碼,加上點中文。如下:

namespace WebService1

{

    ///<summary>

    /// Service1的摘要說明

    ///</summary>

    [WebService(Namespace ="http://tempuri.org/")]

    [WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]

    // 若要允許使用 ASP.NET AJAX從指令碼中調用此 Web 服務,請取消對下行的注釋。

    //[System.Web.Script.Services.ScriptService]

    public class Service1 : System.Web.Services.WebService

    {

 

        [WebMethod]

        public string HelloWorld()

        {

            return"Hello World你好";

        }

    }

}

五,進行用戶端gaosp操作:

1,建立檔案夾client/gsoap。並把gsoap-2.7/gsoap/下的stdsoap2.cpp和stdsoap2.h拷貝至剛剛建立的gsoap檔案夾下。另外還要考幾個檔案gsoap/soapC.cpp ,gsoap/soapClient.cpp ,有需要的自己繼續拷貝,我測試就這幾個夠了。

2,wsdl2h –I "E:/gsoap/source/gsoap-2.7/gsoap/WS"-s -o test.h http://localhost:5925/Service1.asmx?WSDL(加-s是表示不使用stl庫)

3, 執行soapcpp2  -I    "E:/gsoap/source/gsoap-2.7/gsoap/import"test.h

4, 編寫test.cpp檔案

#include <QApplication>

#include <QPushButton>

#include <QLineEdit>

#include <QHBoxLayout>

#include <QDialog>

#include "gsoap/Service1Soap.nsmap"

#include "gsoap/soapService1SoapProxy.h"

class MyDialog:public QDialog

{

     Q_OBJECT

public: 

     MyDialog(QWidget *parent=0):QDialog(parent)

     {

         btn=newQPushButton("invoke");

         line= new QLineEdit;

         connect(btn,SIGNAL(clicked()),this,SLOT(OnClicked()));

         QHBoxLayout*layout =newQHBoxLayout;

         layout->addWidget(line);

         layout->addWidget(btn);

         setLayout(layout);

     }

private:

     QPushButton*btn;

     QLineEdit*line;

     public slots:

         void OnClicked()

         {

              Service1Soapsoap;

              soap_set_mode(soap.soap,SOAP_C_UTFSTRING);

              _ns1__HelloWorldreq;

              _ns1__HelloWorldResponseres;

              soap.__ns2__HelloWorld(&req,&res);

              QStringstr =QString::fromUtf8(res.HelloWorldResult->c_str());

              line->setText(str);

         }

};

 

#include "test.moc"

 

int main(intargc,char* argv[])

{

     QApplicationapp(argc,argv);

     MyDialogmd;

     md.show();

     return app.exec();

 

}

根據需要自己修改。

六,soap_set_mode(soap.soap, SOAP_C_UTFSTRING);這句是為了顯示中文的,讓字串採用utf8編碼。當我們在使用命令列程式的時候,即使使用了該選項也顯示不出中文,因為命令列顯示的字串必須經過decode成GBK才可以顯示。

七,soapcpp2常用選項

  • -C 僅產生用戶端代碼
  • -S 僅產生伺服器端代碼
  • -L 不要產生soapClientLib.c和soapServerLib.c檔案
  • -c 產生純C代碼,否則是C++代碼(與標頭檔有關)
  • -I 指定import路徑(見上文)
  • -x 不要產生XML樣本檔案
  • -i 產生C++封裝,用戶端為xxxxProxy.h(.cpp),伺服器端為xxxxService.h(.cpp)。

八,產生的用戶端介面在test.h檔案中,可以查看編碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.