2013-05-02 44 views
0

我是webservices的新手。我试图通过这种方式来查看结果:使用参数c调用webservice#

private void Form1_Load(object sender, EventArgs e) 
{ 
    MessageBox.Show(getServiceResult("http://prod.sivaonline.pt/SAG.WS.SIVA.SVOLB2C/ViaturasNovas.asmx?wsdl")); 
} 

public string getServiceResult(string serviceUrl) 
{ 
    HttpWebRequest HttpWReq; 
    HttpWebResponse HttpWResp; 
    HttpWReq = (HttpWebRequest)WebRequest.Create(serviceUrl); 
    HttpWReq.Method = "GetMarcas"; 
    HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); 
    if (HttpWResp.StatusCode == HttpStatusCode.OK) 
    { 
    //Consume webservice with basic XML reading, assumes it returns (one) string 
    XmlReader reader = XmlReader.Create(HttpWResp.GetResponseStream()); 
    while (reader.Read()) 
    { 
     reader.MoveToFirstAttribute(); 
     if (reader.NodeType == XmlNodeType.Text) 
     { 
     return reader.Value; 
     } 
    } 
    return String.Empty; 
    } 
    else 
    { 
    throw new Exception("Error on remote IP to Country service: " + HttpWResp.StatusCode.ToString()); 
    } 
} 

现在,它不给我任何消息框。这是正常的吗?我想添加一些参数,如:

configurador=true 

回答

2

Visual Studio通过在客户端为它们创建代理类来调用Web服务很容易。您创建代理类的对象并调用其各自的方法,这些方法由框架在内部转换为SOAP调用。只需右键单击您的项目并使用添加服务参考而不是使用HttpWebRequest

+0

好的,我做到了。现在,我可以检索一个字符串与XML? – Hahn86 2013-05-02 10:51:32

+0

对于任何可序列化类型,您可以在您的服务中定义一个方法并将其返回类型设置为该方法。为了返回或发送XML,最简单的选择是将函数的参数(或返回)类型设置为字符串。其他更复杂的选项也可用。 – dotNET 2013-05-02 12:32:46

+0

我不理解。正如我所说,我是webservices的新手,我无法更改webservice,我只是想在我的应用程序中访问它,并获取一些数据。例如,GetMarcas方法位于web服务中,并获取服务器数据库中的所有汽车商标,并且该方法具有参数,如“active = true”。我应该如何获得积极的商标? – Hahn86 2013-05-02 13:01:34