1

我正在尝试从使用 ksoap2-android 的 Android 应用程序调用用于海岸向导的官方 D&D4E 角色生成器的 DDI Content Vault服务,但不幸的是,每当我调用 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;
}

如果我可以让登录功能工作,我可以很容易地解决剩下的问题;任何帮助使它工作将不胜感激。

4

1 回答 1

2

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

原来我添加了错误的标题;我正在做的是将它们添加到 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 字段,因为没有它我会收到成功的登录响应。

于 2013-05-28T22:32:14.743 回答