HttpPost

时间:2015-05-16 10:05:58

标签: android http http-headers odata androidhttpclient

我必须将xml有效负载发布到需要身份验证和ODATA的{​​{1}}服务。

我有两个x-csrf-token。在第一个对象中有AsyncTasks个对象,并使用以下代码获取URLConnection

x-csrf-token

现在,在第一个成功完成后立即执行的第二个 URL obj = new URL(Util.ODATA_URL + "SO_BEATPSet"); URLConnection conn = obj.openConnection(); conn.setRequestProperty("Authorization", "Basic " + authStringEnc); conn.addRequestProperty("x-csrf-token", "fetch"); ...... ...... String server = conn.getHeaderField("x-csrf-token"); 中,遇到 403错误。它基本上说我的 x-csrf-token验证失败了

我运行了一个简单的循环测试,在那里我运行了第一次AsyncTask三次,我得到了三个不同的标记。 这就是我认为问题所在。在使用AsyncTask的第二个AsyncTask时,服务器期望的是除已经获取的令牌之外的其他令牌。

我有什么方法可以在同一个电话中获取并传递X-csrf-token? 我的第二个AsyncTask如下所示:

HttpPost

2 个答案:

答案 0 :(得分:1)

我最终能够获得成功的POST结果。但是,解决方案对我来说似乎有点脏。但是,它现在解决了我的问题。

我在doInBackground()

AsyncTask方法中输入了如下代码
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost postRequest = new HttpPost(url);

 String credentials = USERNAME + ":" +PASSWORD;
 String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

  /**---------------------------------------------------------------------------------- **/
  /** THIS CODE BELOW CALLS THE SERVER FOR THE TOKEN AND PASSES THE VALUE TO THE SUBSEQUENT POSTREQUEST CALL.
      BY DOING THIS, THE SERVER IS NOT CALLED AGAIN BEFORE POSTREQUEST, AND USER GETS THE LATEST TOKEN **/
                        {
                            HttpGet httpget = new HttpGet(url);
                            httpget.setHeader("Authorization", "Basic " + base64EncodedCredentials);
                            httpget.setHeader("x-csrf-token", "fetch");

                            System.out.println("request:-------------------");
                            System.out.println(httpget.getRequestLine());
                            Header headers[] = httpget.getAllHeaders();
                            for (Header h : headers) {
                                System.out.println(h.getName() + "---:---- " + h.getValue());
                            }

                            HttpResponse res = httpclient.execute(httpget);                           
                            System.out.println("response:-------------------");
                            System.out.println(res.getStatusLine());

                            headers = res.getAllHeaders();
                            for (Header h : headers) {
                                System.out.println(h.getName() + "---:---- " + h.getValue());
                                if (h.getName().equals("x-csrf-token")) {
                                    X_CSRF_TOKEN = h.getValue();
                                }
                            }
                        }
  /**--------------------------------------------------------------------- **/


    // The main POST REQUEST
     postRequest.addHeader("Authorization", "Basic " + base64EncodedCredentials);
     postRequest.setHeader("x-csrf-token", X_CSRF_TOKEN); //   PASSING THE TOKEN GOTTEN FROM THE CODE ABOVE


     StringEntity entity = new StringEntity(myString);
     entity.setContentType(new BasicHeader("Content-Type",
                                "application/atom+xml"));
     postRequest.setEntity(entity);


     HttpResponse response = httpclient.execute(postRequest);
     Log.d("Http Post Response:", response.toString());

     String result = EntityUtils.toString(response.getEntity());
     Log.d("Http Response:", result);

     int responseCode = response.getStatusLine().getStatusCode();
     Log.d("Http Response: ", "Response code " + responseCode);

正如我在代码注释中所解释的那样,代码再次调用服务器,而HttpPost已经创建了一个并获取最新的令牌,然后将其传递给后续的POST请求。

答案 1 :(得分:0)

您是否尝试过编码或解码x-csrf-token?

String tokenEncoded = URLEncoder.encode("xcsrftoken", "UTF-8")

String tokenDecoded = URLDecoder.decode("xcsrftoken", "UTF-8");

我曾经有类似的问题,使用编码为我做了诀窍。