【问题标题】:Android http-posting an xml to wcf server gives Request ErrorAndroid http-posting an x​​ml to wcf server 给出请求错误
【发布时间】:2012-08-03 15:38:53
【问题描述】:

我正在尝试在 Android 上创建一个登录活动,通过 xml 与 wcf 服务器进行通信。

Get Requests 没有问题,但是 Post 却给出了 Request Error。 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; }
}

IService:

[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
    }

客户端部分:

XmlGenerator:

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;

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

【问题讨论】:

  • 请将解决方案作为答案并标记为已接受,以便关闭问题。谢谢:)
  • 做到了,谢谢提示:)

标签: android xml wcf http-post


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    XmlGenerator 中必须在标签前添加以下内容:

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

    还有一行

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

    使用 url 参数而不是数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2014-06-22
      • 1970-01-01
      相关资源
      最近更新 更多