本文給出了一個在C++中調用C# WEB Service的例子,如果有什么錯誤,請指出。
首先把c#的webservice給出來:
namespace SecurityWebService
{
public struct SecurityInfo
{
public string Code;
public string CompanyName;
public double Price;
}
public class Service1 : System.Web.Services.WebService
{
private SecurityInfo Security;
public Service1()
{
Security.Code = "";
Security.CompanyName = "";
Security.Price = 0;
}
private void AssignValues(string Code)
{
Security.Code = Code;
Security.CompanyName = Code + " Pty Ltd";
Random RandomNumber = new System.Random();
Security.Price = double.Parse(new System.Random(RandomNumber.Next(1,10)).NextDouble().ToString("##.##"));
}
[WebMethod(Description="dgdfgfdgdfgfdg",EnableSession=false)]
public SecurityInfo GetSecurityInfo(string Code)
{
AssignValues(Code);
SecurityInfo SecurityDetails = new SecurityInfo();
SecurityDetails.Code = Security.Code;
SecurityDetails.CompanyName = Security.CompanyName;
SecurityDetails.Price = Security.Price;
return SecurityDetails;
}
}
然后新建個vs.net項目,在該項目里添加上面的webservice,名字空間就默然為localhost.
然后在cpp文件中添加#include "localhost.h"; using namespace localhost;
最后可以在.h文件文件加上點擊事件:
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
localhost::Service1 * jj = new localhost::Service1;
localhost::SecurityInfo *cc = jj->GetSecurityInfo("111");
textBox1->Text = cc->Code;
textBox2->Text = cc->CompanyName;
textBox3->Text = Convert::ToString(cc->Price);
}
運行后會出現 請求因 HTTP 狀態 401 失敗:Access Denied 的錯誤。
最后我在iis信息服務器上把匿名訪問和允許iis控制密碼給勾上以后就好用了,究竟為什么,希望路過者給出建議?
新聞熱點
疑難解答
圖片精選