【问题标题】:Can't pass java parameters in the same format as c# for WCF service无法为 WCF 服务传递与 c# 相同格式的 java 参数
【发布时间】:2011-09-10 21:22:19
【问题描述】:

这是场景:我有一个正在运行的 WCF 服务,它使用 C# 与此方法进行通信:

 public bool ValidateUser(UserPass up)
    {
        initializeAttributes();
        IMembershipService Member = new AccountMembershipService();
        bool login = Member.ValidateUser(up.User, up.Pass);
        return login;
    }

参数被封装在这个类中:

[DataContract]
public class UserPass
{
    string user = "";
    string pass = "";
    string email = "";

    [DataMember]
    public string User
    {
        get { return user; }
        set { user = value; }
    }

    [DataMember]
    public string Pass
    {
        get { return pass; }
        set { pass = value; }
    }

    [DataMember]
    public string Email
    {
        get { return email; }
        set { email = value; }
    }
}

现在,我想通过 Android 应用程序连接到服务器,现在,我的问题是,如何在 Java 中复制 UserPass 类,以便 ValidateUser 方法可以以它可以理解的方式接收其参数。

作为参考,这是我获取用户和密码的代码:

private void validateUser(String user, String pass)
{

    String SOAP_ACTION = "http://tempuri.org/IUserService/ValidateUser/";
    String METHOD_NAME = "ValidateUser";
    String NAMESPACE = "http://tempuri.org/";
    String URL = "http://10.0.2.2/UserService.svc";

    AlertDialog popup;

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty(user, pass);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.bodyOut = request;
    envelope.dotNet = true;

    HttpTransportSE httpTransport = new HttpTransportSE(URL);

    try
    {
        httpTransport.call(SOAP_ACTION, envelope); //here's the exception!!
        Object response = envelope.getResponse();

        popup = createAlertDialog("Respuesta",response.toString(),"OK");
        popup.show();
    }
    catch (Exception exception)
    {
        String exceptionStr=exception.toString();

        popup = createAlertDialog("Exception!!",exceptionStr,"OK"); 
        popup.show();
    }       
}

它抛出的异常是xmlpullparserexception,据我了解,是因为请求的参数和实际方法不匹配造成的。

非常感谢您阅读我的问题,还有更多可以回答的人:)

编辑:

我终于知道如何比较 XML... 现在,这就是我的 SOAP 所提供的:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:d="http://www.w3.org/2001/XMLSchema" 
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
    <ValidateUser xmlns="http://tempuri.org/" id="o0" c:root="1">
        <User i:type="d:string">someuser</User>
        <Pass i:type="d:string">somepass</Pass>
    <Email i:type="d:string"></Email>
    </ValidateUser>
</v:Body>

这就是它应该做的(从 Visual Studio 2010 的 WCF 测试客户端应用程序中检索):

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IUserService/ValidateUser</Action>
  </s:Header>
  <s:Body>
    <ValidateUser xmlns="http://tempuri.org/">
      <up xmlns:d4p1="http://schemas.datacontract.org/2004/07/LiveAndesWCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <d4p1:Email i:nil="true" />
        <d4p1:Pass>somepass</d4p1:Pass>
        <d4p1:User>someuser</d4p1:User>
      </up>
    </ValidateUser>
  </s:Body>
</s:Envelope>

现在,我不知道如何编写我的肥皂代码以使其生成像后一个那样的 xml 文件。

再次感谢。

【问题讨论】:

  • 显然存在差异,但了解服务对客户的要求会有所帮助。您可以从 WCF 服务生成 WSDL 吗?
  • 感谢您的帮助。但我放弃了这一点,而是改为使用现在可以使用的基于 REST 的服务。

标签: c# java android wcf soap


【解决方案1】:

您是否尝试过查看由soap 调用创建的xml?您可以将其与 .net 代理创建的 xml 进行比较。也许这有助于找到解决方案。

以下是启用soap调用记录的方法: http://msdn.microsoft.com/en-us/library/ms730064.aspx

【讨论】:

    【解决方案2】:

    我觉得这条线很可疑:

    request.addProperty(user, pass);
    

    据我所知,SoapObject 来自 KSOAP2 库,根据文档, addProperty 采用属性名称和值。要设置用户并通过,我希望更像这样:

    request.addProperty("user", user);
    request.addProperty("pass", pass);
    

    目前,您似乎正在添加一个使用user 参数值命名的属性。如果端点需要至少 2 个参数,那么这可能是您不匹配的根源。

    另外,来自UserPass 包装类的值“Email”是可选的吗?因为我没有看到它被设置在任何地方,并且包装类表明它是 SOAP 请求所需要的

    【讨论】:

    • 谢谢!!,我把你提到的那一行改成了:request.addProperty("User", user); request.addProperty("通过", 通过); request.addProperty("Email","");,不幸的是,在同一个地方抛出了同样的异常:(
    猜你喜欢
    • 2017-11-29
    • 2012-01-07
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2016-08-13
    • 2023-04-02
    • 2011-06-02
    • 2016-03-30
    相关资源
    最近更新 更多