gpt4 book ai didi

android - 使用 ksoap 访问 .Net Web 服务时遇到问题

转载 作者:行者123 更新时间:2023-11-29 21:42:01 25 4
gpt4 key购买 nike

我正在尝试调用 DDI Content Vault服务,用于 Wizard's of the Coast 的官方 D&D4E Character Builder,来自使用 ksoap2-android 的 Android 应用程序,但不幸的是,每当我调用登录方法时,我都会收到 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;
}

如果我能让登录功能正常工作,我就可以很容易地搞定其余部分;任何帮助让它工作的人都将不胜感激。

最佳答案

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

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

替换:

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();

并添加此函数来构建 header :

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

关于android - 使用 ksoap 访问 .Net Web 服务时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16800112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com