【问题标题】:Trouble accessing .Net web service using ksoap使用 ksoap 访问 .Net Web 服务时遇到问题
【发布时间】:2013-05-28 19:40:35
【问题描述】:

我正在尝试从使用 ksoap2-android 的 Android 应用程序调用 DDI Content Vault 服务,该服务用于海岸向导的官方 D&D4E 角色生成器,但不幸的是,每当我调用 Login 方法时,我都会收到 HTTP 状态 415 .由于我试图访问别人的服务,我无法控制服务器端,只能与客户端工作。

这是一个从 Windows 应用发送的有效请求:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
        <a:Action s:mustUnderstand="1">http://tempuri.org/IContentVaultService/Login</a:Action>
        <a:MessageID>urn:uuid:f12a29a6-0421-457a-b4e0-8d0c189907d1</a:MessageID>
        <a:ReplyTo>
            <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <a:To s:mustUnderstand="1">http://ioun.wizards.com/ContentVault.svc</a:To>
    </s:Header>
    <s:Body>
        <Login xmlns="http://tempuri.org/">
            <userName>Redacted, string</userName>
            <password>Redacted, byte array</password>
        </Login>
    </s:Body>
</s:Envelope>

这是响应,表明登录成功:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
        <a:Action s:mustUnderstand="1">http://tempuri.org/IContentVaultService/LoginResponse</a:Action>
        <a:RelatesTo>urn:uuid:f12a29a6-0421-457a-b4e0-8d0c189907d1</a:RelatesTo>
    </s:Header>
    <s:Body>
        <LoginResponse xmlns="http://tempuri.org/">
            <LoginResult>true</LoginResult>
        </LoginResponse>
    </s:Body>
</s:Envelope>

这是我正在使用的代码:

private static final String SOAP_ACTION = "http://tempuri.org/IContentVaultService/Login";
private static final String METHOD_NAME = "Login";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://ioun.wizards.com/ContentVault.svc";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapObject login = new SoapObject(NAMESPACE, "Login");
PropertyInfo name = new PropertyInfo();
name.setName("userName");
name.setValue(uName);
name.setType(String.class);

PropertyInfo pWord = new PropertyInfo();
pWord.setName("password");
pWord.setValue(simpleEncrypt(pass, uName));
pWord.setType(new byte[0].getClass());

request.addProperty(name);
request.addProperty(pWord);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
new MarshalBase64().register(envelope);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

List<HeaderProperty> headerProperty = new ArrayList<HeaderProperty>();
headerProperty.add(new HeaderProperty("Action", SOAP_ACTION));
headerProperty.add(new HeaderProperty("To", URL));
headerProperty.add(new HeaderProperty("ReplyTo", "http://www.w3.org/2005/08/addressing/anonymous"));

HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope, headerProperty);

如果有人需要,这里是密码的编码方式;它给出的结果与我在其他平台上已知的好版本完全相同,所以我知道它工作正常。是的,我知道这不是一种好的做事方式,但我不是设计这该死的东西的人。

private byte[] simpleEncrypt(String value, String key)
{
    MessageDigest digest = null;
    byte[] hash = null;
    byte[] IV = null;
    try
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

        digest = MessageDigest.getInstance("SHA-256");
        digest.reset();
        hash = digest.digest(key.getBytes("UTF-8"));
        IV = Arrays.copyOfRange(hash, 0, cipher.getBlockSize());
        SecretKey secret = new SecretKeySpec(hash, "AES");
        IvParameterSpec ivspec = new IvParameterSpec(IV);
        cipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
        return cipher.doFinal(value.getBytes("UTF-8"));
    }
    catch (Exception e1)
    {
        statusBar.setVisibility(View.VISIBLE);
        progress.setVisibility(View.INVISIBLE);
        status.setText(e1.getLocalizedMessage());
    }
    return null;
}

如果我能让登录功能正常工作,我就可以轻松搞定其余部分;任何帮助使其工作将不胜感激。

【问题讨论】:

    标签: android .net ksoap


    【解决方案1】:

    愚蠢的我,回答我自己的问题......

    原来我添加了错误的标题;我正在做的是将它们添加到 HTTP 标头中,当我需要将其添加到 SOAP XML 文档时。

    替换:

    List<HeaderProperty> headerProperty = new ArrayList<HeaderProperty>();
    headerProperty.add(new HeaderProperty("Action", SOAP_ACTION));
    headerProperty.add(new HeaderProperty("To", URL));
    headerProperty.add(new HeaderProperty("ReplyTo", "http://www.w3.org/2005/08/addressing/anonymous"));
    

    与:

    envelope.headerOut = buildHeader();
    

    并添加此函数来构建标题:

            private Element[] buildHeader() {
                List<Element> headers = new ArrayList<Element>();
                Element action = new Element().createElement("http://www.w3.org/2005/08/addressing", "Action");
                action.addChild(Node.TEXT, SOAP_ACTION);
                action.setAttribute("http://www.w3.org/2005/08/addressing", "mustUnderstand", "1");
                headers.add(action);
    
                Element to = new Element().createElement("http://www.w3.org/2005/08/addressing", "To");
                to.addChild(Node.TEXT, URL);
                to.setAttribute("http://www.w3.org/2005/08/addressing", "mustUnderstand", "1");
                headers.add(to);
    
                Element replyto = new Element().createElement("http://www.w3.org/2005/08/addressing", "ReplyTo");
                Element address = new Element().createElement("http://www.w3.org/2005/08/addressing", "Address");
                replyto.addChild(Node.ELEMENT, address);
                address.addChild(Node.TEXT, "http://www.w3.org/2005/08/addressing/anonymous");
                replyto.setAttribute("http://www.w3.org/2005/08/addressing", "mustUnderstand", "1");
                headers.add(replyto);
    
                int size = headers.size();
                Element[] array = new Element[size];
                for (int i=0;i<size;i++)
                    array[i] = headers.get(i);
                return array;
            }
    

    附带说明一下,实际上不需要 MessageID 字段,因为没有它我会收到成功的登录响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 2012-10-27
      相关资源
      最近更新 更多