/**
  * @param apkFileUrl 形如:http://more.tianjimedia.com/software/download.jhtml?from=baidu&id=32570614
  * @return
  */
 public static String getApkFileUrl(String apkFileUrl) {
   // 从tianjimedia的下载链接会转向到一个下载地址
   // http://tj3.mydown.yesky.com/sjsoft/adi/1/Talking TransFormers Free.apk
   // 但是下载地址中含有空格会抛出异常
   // 这里主要是为了探测已经获取到得下载链接在转向后是否会出现空格抛出异常的情况
   HttpClient client = ClientManager.newHttpClient(); // 这里采用new一个client
   HttpGet httpGet = ClientManager.createHttpGet(apkFileUrl);
   try {
     HttpResponse response = client.execute(httpGet);
     httpGet.abort();
     ClientManager.consume(response);
   } catch (ClientProtocolException e) {
     if (e.getCause() != null) {
       String realURL = e.getCause().getMessage();
       if (realURL != null) {
         int index = realURL.indexOf("http:");
         if (index != -1) {
           return realURL.substring(index);
         }
       }
     }
   } catch (Exception e) {
   } finally {
     ClientManager.shutdown(client);
   }
   return apkFileUrl;
 }
  @Test(expected = ClientProtocolException.class)
  public void testBasicAuthenticationFailureOnNonRepeatablePost() throws Exception {
    this.localServer.register("*", new AuthHandler());
    this.localServer.start();

    TestCredentialsProvider credsProvider =
        new TestCredentialsProvider(new UsernamePasswordCredentials("test", "test"));

    this.httpclient.setCredentialsProvider(credsProvider);

    HttpPost httppost = new HttpPost("/");
    httppost.setEntity(
        new InputStreamEntity(
            new ByteArrayInputStream(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}), -1));

    try {
      this.httpclient.execute(getServerHttp(), httppost);
      Assert.fail("ClientProtocolException should have been thrown");
    } catch (ClientProtocolException ex) {
      Throwable cause = ex.getCause();
      Assert.assertNotNull(cause);
      Assert.assertTrue(cause instanceof NonRepeatableRequestException);
      throw ex;
    }
  }
예제 #3
0
  /**
   * Hits the logout service and then the login.
   *
   * <p>Returns - userid - 0 if already logged in
   */
  public int authenticate()
      throws HttpAuthenticationFailedException, IOException, JSONException, NoAccountException {
    RestClient authClient = new RestClient();

    int userId = 0;

    try {
      // Log.i(TAG, "Routine logout attempt");
      authClient.authpost(wsUserLogoutUrl);
    } catch (Exception e) {
      Log.i(TAG, "Exception on logout - not to worry: " + e.toString());
      // We don't care a lot about this, as we were just trying to ensure clean login.
    }

    try {
      List<NameValuePair> credentials = getCredentialsFromAccount();

      // Log.i(TAG, "Normal login attempt after logout credentials=" + credentials.toString());
      JSONObject authResult = authClient.authpost(wsUserAuthUrl, credentials);

      userId = authResult.getJSONObject("user").getInt("uid");
      Host profileInfo = Host.CREATOR.parse(authResult.getJSONObject("user"));
      MemberInfo.initInstance(profileInfo);

      String cookieSessionName = authResult.getString("session_name");
      String cookieSessionId = authResult.getString("sessid");

      AuthenticationHelper.addCookieInfo(cookieSessionName, cookieSessionId, userId);

      String filePath = MemberInfo.getMemberPhotoFilePath();
      // Get the member photo if it doesn't exist already
      File profileImageFile = new File(filePath);
      // If the file doesn't exist or is tiny, download it, otherwise use the one we have
      if (!profileImageFile.exists() || profileImageFile.length() < 1000) {
        // Download it
        downloadMemberPhoto(profileInfo, filePath);
      }

    } catch (ClientProtocolException e) {
      if (e.getCause() instanceof CircularRedirectException) {
        // If we get this authentication has still been successful, so ignore it
      } else {
        throw new HttpAuthenticationFailedException(e);
      }
    } catch (IOException e) {
      // Rethrow, prevent the catch below from getting to it. we want to know this was IO exception
      throw e;
    } catch (HttpAuthenticationFailedException e) { // Attempting to do auth with wrong credentials
      throw e;
    } catch (NoAccountException e) {
      throw e;
    } catch (HttpException e) {
      if (e.getMessage().equals("406")) {
        Log.i(TAG, "Got error 406 attempting to log in, so ignoring");
        // This is the case where we hit auth and were already authenticated... but shouldn't have
        // happened.
      } else {
        throw e;
      }
    } catch (Exception e) {
      // We might have had a json parsing or access exception - for example, if the "user" was not
      // there,
      // Could also have AuthenticatorException or OperationCancelledException here
      // or if there was something wrong with what the server returned
      throw new HttpAuthenticationFailedException(e);
    }
    return userId;
  }