无法使用kso​​ap访问.Net Web服务

时间:2013-05-28 19:40:35

标签: android .net ksoap

我正在尝试使用kso​​ap2-android从Android应用程序调用DDI Content Vault服务,用于Coast的官方D& D4E角色生成器的向导,但不幸的是,每当我持续获取HTTP状态415调用Login方法。由于我正在尝试访问其他人的服务,因此我无法控制服务器端,并且只能与客户端协同工作。

这是一个从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;
}

如果我可以使登录功能正常工作,我可以轻松地完成剩下的工作;任何有助于实现这一目标的帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

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

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

替换:

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字段,因为我收到了没有它的成功登录响应。