2013-05-18 77 views
0

我需要通過發出發佈請求來驗證用戶。我張貼我的XML數據使用REST協議,但得到403錯誤。請幫助我找出我做錯了什麼,下面是我的代碼發佈。在android中,我發佈xml數據到服務器,但得到403錯誤

public void makePostConnection(String fileUrl, 
      HashMap<String, String> requestData) { 
     URL url; 
     HttpURLConnection urlConnection; 
     try { 
      url = new URL(fileUrl); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setRequestProperty("Content-Type", 
        "application/x-www-form-urlencoded"); 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(fileUrl); 
      httppost.addHeader("accept", "text/xml"); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        requestData.size()); 
      System.out.println("Size= " + requestData.size()); 

      for (Entry<String, String> entry : requestData.entrySet()) { 
       nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry 
         .getValue())); 
       System.out.println("Key= " + entry.getKey() + " Value= " 
         + entry.getValue()); 
      } 
      System.out.println("Request ba= " + nameValuePairs.toString()); 
      StringEntity requestBody = new StringEntity(
        nameValuePairs.toString()); 
      // requestBody.setContentType("text/xml"); 
      // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httppost.setEntity(requestBody); 
      // httppost.set 
      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 
      int code = response.getStatusLine().getStatusCode(); 

      System.out.println("Code= " + code); 

     } catch (MalformedURLException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } 
+0

403錯誤意味着它是一個身份驗證問題。服務器是否需要認證? – edwardmp

+0

準確地說我的服務器需要一個認證令牌我得到它並使我的請求正文如下:[token = bEZ9C61PLg56yz81LM1yo9S,imeino = 12345676543456543234,[email protected],mobileno = 1447525863] –

+0

檢查服務器是否足夠以驗證.. – edwardmp

回答

1

喜我解決我的問題,通過改變一些線I設置 httppost.addHeader( 「內容類型」, 「應用程序/ x WWW的形式進行了urlencoded」); 並通過這樣做一個urlencoded請求 UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8); 以下是我的所有代碼,使成功的請求。

try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(fileUrl); 
      httppost.addHeader("Content-Type", 
        "application/x-www-form-urlencoded"); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        requestData.size()); 
      System.out.println("Size= " + requestData.size()); 

      for (Entry<String, String> entry : requestData.entrySet()) { 
       nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry 
         .getValue())); 
       System.out.println("Key= " + entry.getKey() + " Value= " 
         + entry.getValue()); 
      } 

      UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs, 
        HTTP.UTF_8); 
      httppost.setEntity(ent); 
      HttpResponse response = httpclient.execute(httppost); 
      int code = response.getStatusLine().getStatusCode(); 

      System.out.println("Code= " + code); 

     } catch (MalformedURLException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } 
相關問題