首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

WebService调用的奇怪有关问题

2012-02-21 
WebService调用的奇怪问题要做一个获取天气信息的东西,同事给了一个站点:http://webservice.webxml.com.cn

WebService调用的奇怪问题
要做一个获取天气信息的东西,同事给了一个站点:

http://webservice.webxml.com.cn/webservices/weatherws.asmx?wsdl

用WSDL Importer自动生成了下面的两个文件:

第一个weatherws.cpp文件

C/C++ code
#include <vcl.h>#pragma hdrstop#if !defined(weatherwsH)#include "weatherws.h"#endifnamespace NS_weatherws {_di_WeatherWSSoap GetWeatherWSSoap(bool useWSDL, AnsiString addr){  static const char* defWSDL= "http://webservice.webxml.com.cn/webservices/weatherws.asmx?wsdl";  static const char* defURL = "http://webservice.webxml.com.cn/webservices/weatherws.asmx";  static const char* defSvc = "WeatherWS";  static const char* defPrt = "WeatherWSSoap";  if (addr=="")    addr = useWSDL ? defWSDL : defURL;  THTTPRIO* rio = new THTTPRIO(0);  if (useWSDL) {    rio->WSDLLocation = addr;    rio->Service = defSvc;    rio->Port = defPrt;  } else {    rio->URL = addr;  }  _di_WeatherWSSoap service;  rio->QueryInterface(service);  if (!service)    delete rio;  return service;}static void RegTypes(){  /* WeatherWSSoap */  InvRegistry()->RegisterInterface(__interfaceTypeinfo(WeatherWSSoap), L"http://WebXml.com.cn/", L"utf-8");  InvRegistry()->RegisterDefaultSOAPAction(__interfaceTypeinfo(WeatherWSSoap), L"http://WebXml.com.cn/%operationName%");  /* getRegionDatasetResult */  RemClassRegistry()->RegisterXSClass(__classid(getRegionDatasetResult), L"http://WebXml.com.cn/", L"getRegionDatasetResult");  RemClassRegistry()->RegisterXSInfo(__arrayTypeinfo(ArrayOfString), L"http://WebXml.com.cn/", L"ArrayOfString");  /* getSupportCityDatasetResult */  RemClassRegistry()->RegisterXSClass(__classid(getSupportCityDatasetResult), L"http://WebXml.com.cn/", L"getSupportCityDatasetResult");}#pragma startup RegTypes 32};     // NS_weatherws
 

第二个weatherws.h文件

C/C++ code
#ifndef   weatherwsH#define   weatherwsH#include <System.hpp>#include <InvokeRegistry.hpp>#include <XSBuiltIns.hpp>#include <SoapHTTPClient.hpp>namespace NS_weatherws {// ************************************************************************ //// The following types, referred to in the WSDL document are not being represented// in this file. They are either aliases[@] of other types represented or were referred// to but never[!] declared in the document. The types from the latter category// typically map to predefined/known XML or Borland types; however, they could also // indicate incorrect WSDL documents that failed to declare or import a schema type.// ************************************************************************ //// !:string          - "http://WebXml.com.cn/"// !:string          - "http://www.w3.org/2001/XMLSchema"class  getRegionDatasetResult;class  getSupportCityDatasetResult;// ************************************************************************ //// Namespace : http://WebXml.com.cn/// ************************************************************************ //class getRegionDatasetResult : public TRemotable {private:  AnsiString      Fs_schema;public:__published:  __property AnsiString   s_schema = { read=Fs_schema, write=Fs_schema };};typedef DynamicArray<AnsiString>  ArrayOfString;  /* "http://WebXml.com.cn/" */// ************************************************************************ //// Namespace : http://WebXml.com.cn/// ************************************************************************ //class getSupportCityDatasetResult : public TRemotable {private:  AnsiString      Fs_schema;public:__published:  __property AnsiString   s_schema = { read=Fs_schema, write=Fs_schema };};// ************************************************************************ //// Namespace : http://WebXml.com.cn/// soapAction: http://WebXml.com.cn/%operationName%// transport : http://schemas.xmlsoap.org/soap/http// binding   : WeatherWSSoap// service   : WeatherWS// port      : WeatherWSSoap// URL       : http://webservice.webxml.com.cn/webservices/weatherws.asmx// ************************************************************************ //__interface INTERFACE_UUID("{54393593-AC04-4DAB-BBF6-AB948B692BED}") WeatherWSSoap : public IInvokable{public:  virtual getRegionDatasetResult* getRegionDataset() = 0;   virtual ArrayOfString   getRegionProvince() = 0;   virtual ArrayOfString   getRegionCountry() = 0;   virtual getSupportCityDatasetResult* getSupportCityDataset(const AnsiString theRegionCode) = 0;   virtual ArrayOfString   getSupportCityString(const AnsiString theRegionCode) = 0;   virtual ArrayOfString   getWeather(const AnsiString theCityCode, const AnsiString theUserID) = 0; };typedef DelphiInterface<WeatherWSSoap> _di_WeatherWSSoap;_di_WeatherWSSoap GetWeatherWSSoap(bool useWSDL=false, AnsiString addr="");#endif // __weatherws_h__};     // NS_weatherws#if !defined(NO_IMPLICIT_NAMESPACE_USE)using  namespace NS_weatherws;#endif  



然后写了下面的代码:

C/C++ code
void __fastcall TForm1::Button1Click(TObject *Sender){  _di_WeatherWSSoap test;  test = GetWeatherWSSoap(true,"http://webservice.webxml.com.cn/webservices/weatherws.asmx?wsdl");  TStringDynArray text = [color=#FF0000]test->getRegionProvince()[/color];  Memo1->Lines->Clear();  for(int i=0;i<text.get_length();i++)  {    Memo1->Lines->Add(text[i]);  }    }



问题是这样的:
就在这段自己写的代码里面,test有四个函数可以使用,前面两个test->getRegionProvince()和test->getRegionCountry() 都可以正常使用,有返回值,但是后面两个test->getSupportCityString("311102")和test->getWeather("311102","")都不能正常使用,这里的参数我都写了,311102是上海的ID,getWeather第二个参数为空代表免费用户,就是这两个函数无法使用,没有正常的返回值,总是提示参数不能为空,可是我明明传递了参数,怎么会这样呢?

请大侠能帮助我,把这段代码调试一下,看看问题究竟出在哪里,非常感谢!!!

[解决办法]
试了一下没问题,test->getSupportCityString得到以下结果:
宝山,2009
崇明,2012
奉贤,2063
嘉定,2011
金山,3530
闵行,2008
南汇,2014
浦东,2015
青浦,2061
上海,2013
松江,3413

getWeather也没有问题,但没查到天气,返回:
查询结果为空。http://www.webxml.com.cn/

不知道你用的是哪个版本,很多WebServices要求的参数和返回类型是UTF8类型字符串
[解决办法]
在CPP文件中增加如下
//add,支持中文
rio->Converter->Options<<soUTF8InHeader;
rio->HTTPWebNode->UseUTF8InHeader = true;

在RegTypes()函数中增加如下
//add
InvRegistry()->RegisterInvokeOptions(__interfaceTypeinfo(WeatherWSSoap), ioDocument);

热点排行