使用C++.NET调用webservice实战

来源:互联网 发布:23端口是什么 编辑:程序博客网 时间:2024/06/05 19:32






使用C++.NET调用webservice实战

以前看过很多用非托管代码实现的调用WEB服务,今天自己亲自用托管C++来做一下,毕竟以前没有专门学过C++,一直用C#了,这回返回来学习下C++.NET。

http://hi.baidu.com/44498/item/d0769c8cb692b8c299255f44

首先打开VS2008,然后新建一个项目,选择CLR,然后选择Windows窗体应用程序。

然后就OK了。接下来,拖几个控件上去,方便显示。。。简单的,两个button,一个textBox就行了。

然后双击第一个按钮,进入以下界面。。。能看到button_click按钮单机事件,在里面写代码。

然后先别动它,双击第二个按钮,在第二个按钮单击事件里写this->Close();关闭窗体:

接下来,就是要添加WEB服务了。。。对着项目点右键,添加web引用。。。

按照箭头顺序来,即可。

然后你会发现项目里多了一些文件。。。

接下来。。。什么也不用做,写代码吧:

在button1的click事件里写如下代码:

    WeatherService::Service^ WS = gcnew WeatherService::Service ();

    array<Object^>^ myarr = WS->getWeatherbyCityName ("北京",WeatherService::theDayFlagEnum ::Today);
    
   
    for(int i =1; i <7 ; i ++)
    {
     textBox1->Text += myarr[i];
    }

然后,F6,编译,CTRL+F5运行。。。100%成功。。。效果如下:

参考MSDN:

using namespace System;
void main1(); void main2();
void main()
{
   main1();
   Console::WriteLine();
   main2();
}

void PrintValues( array<Object^>^myArr );
void PrintValues( array<int>^myArr );
void main1()
{
   // Creates and initializes a new int array and a new Object array.
   array<int>^myIntArray = {1,2,3,4,5};
   array<Object^>^myObjArray = {26,27,28,29,30};

   // Prints the initial values of both arrays.
   Console::WriteLine( "Initially," );
   Console::Write( "int array:   " );
   PrintValues( myIntArray );
   Console::Write( "Object array:" );
   PrintValues( myObjArray );

   // Copies the first two elements from the int array to the Object array.
   Array::Copy( myIntArray, myObjArray, 2 );

   // Prints the values of the modified arrays.
   Console::WriteLine( "\nAfter copying the first two elements of the int array to the Object array," );
   Console::Write( "int array:   " );
   PrintValues( myIntArray );
   Console::Write( "Object array:" );
   PrintValues( myObjArray );

   // Copies the last two elements from the Object array to the int array.
   Array::Copy( myObjArray, myObjArray->GetUpperBound( 0 ) - 1, myIntArray, myIntArray->GetUpperBound( 0 ) - 1, 2 );

   // Prints the values of the modified arrays.
   Console::WriteLine( "\nAfter copying the last two elements of the Object array to the int array," );
   Console::Write( "int array:   " );
   PrintValues( myIntArray );
   Console::Write( "Object array:" );
   PrintValues( myObjArray );
}

void PrintValues( array<Object^>^myArr )
{
   for ( int i = 0; i < myArr->Length; i++ )
   {
      Console::Write( "\t{0}", myArr[ i ] );

   }
   Console::WriteLine();
}

void PrintValues( array<int>^myArr )
{
   for ( int i = 0; i < myArr->Length; i++ )
   {
      Console::Write( "\t{0}", myArr[ i ] );

   }
   Console::WriteLine();
}


/* 
This code produces the following output.

Initially,
int array:       1    2    3    4    5
Object array:    26    27    28    29    30
After copying the first two elements of the int array to the Object array,
int array:       1    2    3    4    5
Object array:    1    2    28    29    30
After copying the last two elements of the Object array to the int array,
int array:       1    2    3    29    30
Object array:    1    2    28    29    30
*/

原创粉丝点击