如何使用以下xml模式对用户进行身份验证

时间:2014-07-22 06:42:09

标签: java android xml web-services basic-authentication

我正在开发从android到C#.net服务的用户登录页面 它只接受XML并且必须使用HTTP POST方法 我能够使用JSON命中服务器并获得响应 但单独的USERNAME和LOGIN凭据coludnt正确传递 我得到了回复

  

输入未处于正确格式

可能有人帮助我

我使用下面的JSON代码得到了服务器的响应:

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://mydomain.com/api.svc/usr/?fmt=json");
        httppost.addHeader("POINT_ID", "WEB02");
        httppost.addHeader("AUTH_CODE", "JAbmQX5pbBpMTF0pMTbCg==");
        List<NameValuePair> paarams = new ArrayList<NameValuePair>();
        paarams.add(new BasicNameValuePair("Mode", "login"));
        paarams.add(new BasicNameValuePair("LoginId", "api@api.in"));
        paarams.add(new BasicNameValuePair("Password", "123456"));
        try {
            httppost.setEntity(new UrlEncodedFormEntity(paarams, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }
        /*
         * Execute the HTTP Request
         */
        try {
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity respEntity = response.getEntity();
            statCode = response.getStatusLine().getStatusCode();
            if (respEntity != null) {
                // EntityUtils to get the response content
                 content = EntityUtils.toString(respEntity);
            }
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
        }

xml如下所示

   <User>
     <Mode>login</Mode>
     <LoginId>api@api.in</LoginId>
     <Password>123456</Password>
     <Name>Guest</Name>
     <SessionId>5</SessionId>
     <Id>7</Id>
     <Guest>Y</Guest>
     <Country>USD</Country>
   </User>

我能够直到用户,但登录名和密码无法正常播放

然后我说api只接受xml 所以我尝试在xml本身中使用以下xml hardcoding 用户名密码

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://mydomain.com/api.svc/usr/?fmt=json");
        httppost.addHeader("POINT_ID", "WEB02");
        httppost.addHeader("AUTH_CODE", "JAbmQX5pbBpMTF0pMTbCg==");
        try {
            StringEntity se = new StringEntity( "<User><Mode>login</Mode><LoginId>api@api.in</LoginId><Password>123456</Password><Name>My API</Name><SessionId>5</SessionId><Id>7</Id><Guest>Y</Guest><Country>USD</Country></User>", HTTP.UTF_8);
            se.setContentType("text/xml");
            httppost.setEntity(se);
            HttpResponse httpresponse = httpclient.execute(httppost);
            HttpEntity resEntity = httpresponse.getEntity();
            statCode = httpresponse.getStatusLine().getStatusCode();
            content=EntityUtils.toString(resEntity);        
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

但是对于上面的xml我只得到请求错误 html页面 使用HTML标记和CSS

我尝试浏览以下链接

  1. http://developer.appcelerator.com/question/148810/send-xml-data-to-webservice
  2. how to post xml data to server in android
  3. Android, send and receive XML via HTTP POST method
  4. http://sunil-android.blogspot.in/2013/04/jquery-ajax-request-and-response.html
  5. Sending HTTP POST Request In Java
  6. Ajax post data in android java
  7. 但大多数链接都是通过用户名密码和api链接 但是api im尝试使用post,beforesend和data 所以有人帮我验证这个api的用户登录

    感谢提前

1 个答案:

答案 0 :(得分:0)

我自己如何得到了问题的答案。 希望它有所帮助 只有一行代码错过了完成工作 添加以下行

    httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");

之前

    httppost.setEntity(se);

在我提出的第二组编码中。

现在我得到了确切的答案,没有任何问题