Advertisement
Guest User

sebi

a guest
Mar 20th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1.  
  2. private static final int CONNECTION_TIMEOUT = 10000;
  3. private static final int DATARETRIEVAL_TIMEOUT = 10000;
  4. private static final String SERVER_URL = "http://www.techtalents.info/login_app.php";
  5. private static String sessionID;
  6.  
  7. private static String requestUrl(String url, String postParameters) {
  8.  
  9. HttpURLConnection urlConnection = null;
  10. try {
  11. URL urlToRequest = new URL(url);
  12. urlConnection = (HttpURLConnection) urlToRequest.openConnection();
  13. urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
  14. urlConnection.setReadTimeout(DATARETRIEVAL_TIMEOUT);
  15.  
  16. if (postParameters != null) {
  17.  
  18.  
  19. urlConnection.setDoOutput(true);
  20. urlConnection.setRequestMethod("POST");
  21. urlConnection.setFixedLengthStreamingMode(
  22. postParameters.getBytes().length);
  23. urlConnection.setRequestProperty("Content-Type",
  24. "application/x-www-form-urlencoded;charset=UTF-8");
  25.  
  26. PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
  27. out.print(postParameters);
  28. out.close();
  29. }
  30.  
  31. int statusCode = urlConnection.getResponseCode();
  32. if (statusCode != HttpURLConnection.HTTP_OK) {
  33.  
  34. }
  35.  
  36.  
  37.  
  38. InputStream in =
  39. new BufferedInputStream(urlConnection.getInputStream());
  40. String respuesta = getResponseText(in);
  41. return respuesta;
  42.  
  43.  
  44.  
  45.  
  46. } catch (MalformedURLException e) {
  47.  
  48. } catch (SocketTimeoutException e) {
  49.  
  50. } catch (IOException e) {
  51.  
  52. } finally {
  53. if (urlConnection != null) {
  54. urlConnection.disconnect();
  55. }
  56. }
  57.  
  58. return null;
  59. }
  60.  
  61. private static String getResponseText(InputStream in) {
  62. try {
  63. Reader reader = new InputStreamReader(in, "UTF-8");
  64. char[]tamanio=new char[6];
  65. reader.read(tamanio);
  66. int tam = Integer.parseInt(new String(tamanio));
  67.  
  68. char[] buffer = new char[tam];
  69. reader.read(buffer);
  70. return new String(buffer);
  71. } catch (UnsupportedEncodingException e) {
  72. e.printStackTrace();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. return "";
  77. }
  78.  
  79. public static boolean login(String username, String password) {
  80. String response = requestUrl(SERVER_URL, "username="+username+"&password="+password);
  81.  
  82. if(response != null && response.startsWith("OK")){
  83. sessionID = response.substring(2);
  84. return true;
  85. }
  86. sessionID=null;
  87. return false;
  88. }
  89.  
  90. public static String post(ArrayList<String> parametros){
  91. StringBuilder sb = new StringBuilder();
  92. if(sessionID == null){
  93. return null;
  94. }
  95. sb.append("sess_id=" + sessionID);
  96. for(int i=0;i<parametros.size();i++){
  97. sb.append("&" + parametros.get(i));
  98. }
  99. String response = requestUrl(SERVER_URL, sb.toString());
  100. Log.i("SERVER:",response);
  101. return response;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement