gsoap 天气预报

  1. mkdir weather
  2. 拷贝gsoap-2.8/gsoap/stdsoap2.c stdsoap2.h 和typemap.dat 到 weather 目录。
  3. wsdl2h -c -o weather.h http://www.webservicex.net/globalweather.asmx?WSDL
  4. soapcpp2 -CL weather.h -I../gsoap-2.8/gsoap/import
  5. vim main_client.c

    
    #include "GlobalWeatherSoap.nsmap"
    
    
    #include "soapH.h"
    
    int main(int argc, char * argv[])
    {
        struct soap * soap = soap_new();
        struct _ns1__GetWeather getweather;
        struct _ns1__GetWeatherResponse getweather_response;
        //char * city = "BeiJing";
        //char * country = "China";
    
        if(argc != 3){
            printf("argc is not 3\n");
            goto OUT;       
        }
        printf("city: %s\n", argv[1]);
        printf("country: %s\n", argv[2]);
        getweather.CityName = argv[1];
        getweather.CountryName = argv[2];
        if( soap_call___ns1__GetWeather(soap, NULL, NULL, &getweather, &getweather_response) == SOAP_OK)
            printf("GetWeatherResponse: \r\n%s\r\n", getweather_response.GetWeatherResult);
        else
            soap_print_fault(soap, stderr);
    OUT:
        soap_destroy(soap);
        soap_end(soap);
        soap_free(soap);
        return 0;
    }
  6. gcc -o weather_client main_client.c soapC.c soapClient.c stdsoap2.c
  7. 编译生产weather_client
  8. ./weather_client beijing china

    city: beijing
    country: china
    GetWeatherResponse: 
    
    <CurrentWeather>
        <Location>Beijing, China (ZBAA) 39-56N 116-17E 55MLocation>
        <Time>Aug 03, 2016 - 10:30 PM EDT / 2016.08.04 0230 UTCTime>
        <Wind> from the ESE (110 degrees) at 4 MPH (4 KT) (direction variable):0Wind>
        <Visibility> 2 mile(s):0Visibility>
        <Temperature> 89 F (32 C)Temperature>
        <DewPoint> 77 F (25 C)DewPoint>
        <RelativeHumidity> 66%RelativeHumidity>
        <Pressure> 29.83 in. Hg (1010 hPa)Pressure>
        <Status>SuccessStatus>
    CurrentWeather>
  9. 也可编写makefile文件
    参考 http://blog.chinaunix.net/uid-21768364-id-4648583.html

    my_server:=calc_server
    my_client:=weather_client
    my_test:=getcity
    
    # your gsoap install directory
    #GSOAP_DIR = /root/Desktop/gsoap-2.8
    GSOAP_DIR = 
    
    # compiler
    CORSS_COMPILER = 
    
    CC=$(CORSS_COMPILER)gcc
    
    # flags
    LIBS:=-lpthread -lm
    INCLS:=-I./ 
    CFLAGS:=-O2 -Wall
    
    COM_SOURCES=soapC.c stdsoap2.c
    COM_OBJS=$(COM_SOURCES:.c=.o)
    SER_SOURCES=main_server.c soapServer.c
    CLI_SOURCES=main_client.c soapClient.c
    TEST_SOURCES=getcity_client.c soapClient.c
    SER_OBJS=$(COM_OBJS) $(SER_SOURCES:.c=.o)
    CLI_OBJS=$(COM_OBJS) $(CLI_SOURCES:.c=.o)
    TEST_OBJS=$(COM_OBJS) $(TEST_SOURCES:.c=.o)
    
    
    all: wsdl gsoap makeClient makeTest
    
    WSDL_FILES := temp.h
    GSOAP_FILES := soapClient.c soapServer.c soapC.c soapH.h soapStub.h
    WSDL_SOURCE := $(wildcard *.wsdl)
    
    wsdl:
    ifeq ($(strip $(WSDL_SOURCE)),)
        @echo not find *.wsdl file.
    else
    ifneq ($(WSDL_FILES), $(wildcard $(WSDL_FILES)))
        wsdl2h -c -o $(WSDL_FILES) $(WSDL_SOURCE)
    endif
    endif
    
    gsoap:
    ifneq ($(GSOAP_FILES), $(wildcard $(GSOAP_FILES)))
        soapcpp2 -CL $(WSDL_FILES) -I../gsoap-2.8/gsoap/import
    endif
    
    makeClient:$(CLI_OBJS)
        $(CC) $(INCLS) $(CFLAGS) -o $(my_client) $^ $(LIBS)
    
    makeServer:$(SER_OBJS)
        $(CC) $(INCLS) $(CFLAGS) -o $(my_server) $^ $(LIBS)
    
    makeTest:$(TEST_OBJS)
        $(CC) $(INCLS) $(CFLAGS) -o $(my_test) $^ $(LIBS)
    
    $(CLI_OBJS):%.o:%.c
        $(CC) $(INCLS) $(CFLAGS) -o $@ -c $^
    $(TEST_OBJS):%.o:%.c
        $(CC) $(INCLS) $(CFLAGS) -o $@ -c $^
    
    clean:
        @rm -rfv *.o *~ *Proxy.h *Object.h $(my_server) $(my_client) $(my_test)
    
    deepclean:
        @rm -rfv *.o *~ *Proxy.h *Object.h $(my_server) $(my_client) $(my_test) *.xml *.nsmap $(GSOAP_FILES) $(WSDL_FILES) 
    
  10. make clean // 清除
    make deepclean // 清除所有,包括wsdl2h和soapcpp2产生的文件
    make // 编译,生成可执行程序 weather_client 和 getcity

  11. vim getcity_client.c

#include "GlobalWeatherSoap.nsmap"
#include "soapH.h"
int main(int argc, char * argv[])
{
    struct soap * soap = soap_new();
    struct _ns1__GetCitiesByCountry getcity;
    struct _ns1__GetCitiesByCountryResponse getcity_response;

    if(argc != 2){
        printf("argc is not 2\n");
        goto OUT;       
    }
    printf("country: %s\n", argv[1]);
    getcity.CountryName = argv[1];
    if( soap_call___ns1__GetCitiesByCountry(soap, NULL, NULL, &getcity, &getcity_response) == SOAP_OK)
        printf("GetCityResponse: \r\n%s\r\n", getcity_response.GetCitiesByCountryResult);
    else
        soap_print_fault(soap, stderr);
OUT:
    soap_destroy(soap);
    soap_end(soap);
    soap_free(soap);
    return 0;
}`
./getcity china
country: china
GetCityResponse: 

  ChinaBeijing
ChinaHohhot
... ChinaQiqihar
ChinaDalian

你可能感兴趣的:(linux,gsoap,wsdl,gsoap)