0

我正在尝试在通过 xml 与 wcf 服务器通信的 Android 上创建登录活动。

获取请求没有问题,但是发布却给出了请求错误。webtracelog 提供以下内容:

“检查 LoginMobileParameter 类型对象的开始元素时出错。根级别的数据无效。第 1 行,位置 1。”

我怀疑xml有问题,但我无法确定。

服务器部分:

登录参数:

[DataContract]
public class LoginMobileParameter
{
    [DataMember(Order = 1)]
    public string Username { get; set; }

    [DataMember(Order = 2)]
    public string Password { get; set; }

    [DataMember(Order = 3)]
    public string DeviceID { get; set; }
}

服务:

[OperationContract]
[WebInvoke(
    UriTemplate = "Security/LoginMobile", 
    Method = "POST", 
    RequestFormat = WebMessageFormat.Xml, 
    ResponseFormat = WebMessageFormat.Xml
)]
string LoginMobile(LoginMobileParameter parameter);

服务:

    public string LoginMobile(string userName, string password, string deviceID)
    {
        string decryptedPw = ServiceFactory.EncryptionService.DecryptString(password);
        bool loginOK = dataService.CheckLogin(userName, decryptedPw);

        if (loginOK)
        {
            Token t = NewToken;
            t.Username = userName;
            t.DeviceID = deviceID;
            string tokenString = dataService.SaveToken(t);

            Log("LoginMobile " + deviceID + ": " + userName + ": Issued Token " + tokenString);
            return tokenString;
        }

        Log("Invalid Username for mobile User " + userName + " / " + password);
        return string.Empty; // not authorized
    }

客户端部分:

xml生成器:

public class XmlGenerator {
    public static String GenerateLoginXml(String user, String password){
        String returnstring = ""; 
        returnstring += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        returnstring += "<LoginMobileParameter><Username>";
        returnstring += user;
        returnstring += "</Username><Password>";
        returnstring += password;
        returnstring += "</Password><DeviceID>androidclient</DeviceID></LoginMobileParameter>";
        return returnstring;
    }
}

登录按钮:

    ...
    String xml = XmlGenerator.GenerateLoginXml(username, password);
    String url = "security/loginmobile";
    String result = con.post(xml, url);

邮政:

    public class HttpConnection {

    public String post(String data, String parameter) {
        String urlToSendRequest = "http://my.server.com:1234/Service/" + parameter;
        String result = "";

        HttpPost httpPost = new HttpPost(urlToSendRequest);

        try {
            StringEntity entity = new StringEntity(parameter, HTTP.UTF_8);
            entity.setContentType("application/xml");
            httpPost.setHeader("Content-Type", "application/xml;charset=UTF-8");
            httpPost.setEntity(entity);

            HttpClient httpClient = new DefaultHttpClient();
             HttpResponse response = httpClient.execute(httpPost);

            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                 result = EntityUtils.toString(responseEntity);
            }
        }

        catch (Exception ex) {
            ex.printStackTrace();
        }
    return result;

我通读了大量线程并尝试了所有方法,但无法使其正常工作。提前致谢

4

2 回答 2

0

尝试发布到 .ashx 并将原始发布的数据写入文本文件。

将该文本文件放入十六进制编辑器,查看 XML 中是否有任何无关字节,例如 BOM。

于 2012-08-03T16:00:25.550 回答
0

必须在标记之前的 XmlGenerator 中添加以下内容:

returnstring += "<LoginMobileParameter xmlns=\"http://schemas.datacontract.org/2004/07/Projectname.Package.Parameter\">";

还有线

StringEntity entity = new StringEntity(parameter, HTTP.UTF_8);

使用 url 参数而不是数据。

于 2012-08-06T15:03:58.620 回答