Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. package com.app.core.HttpClient;
  2.  
  3. import com.app.common.Exeption403;
  4. import com.app.core.HttpClient.proxy.Proxy;
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.UnsupportedEncodingException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import org.apache.http.HttpHost;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.client.HttpClient;
  15. import org.apache.http.client.config.RequestConfig;
  16. import org.apache.http.client.entity.UrlEncodedFormEntity;
  17. import org.apache.http.client.methods.HttpGet;
  18. import org.apache.http.client.methods.HttpPost;
  19. import org.apache.http.impl.client.HttpClientBuilder;
  20. import org.apache.http.message.BasicNameValuePair;
  21. import org.jsoup.Jsoup;
  22. import org.jsoup.nodes.Document;
  23. import org.jsoup.nodes.Element;
  24. import org.jsoup.select.Elements;
  25.  
  26. /**
  27. *
  28. *
  29. * Date: 22.1.2017 (11:03:53)
  30. *
  31. * @author Jakub Löffelmann | jakubloffelmann@gmail.com
  32. *
  33. * Encoding: UTF-8
  34. *
  35. */
  36. public class HttpCore {
  37.  
  38. private final HttpClient client = HttpClientBuilder.create().build();
  39. private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
  40.  
  41. private String cookies;
  42.  
  43. public String getPageContent(String url, Proxy proxy, String referal) throws IOException, Exeption403 {
  44. HttpGet request = new HttpGet(url);
  45. if (proxy != null) {
  46. HttpHost proxyHost = new HttpHost(proxy.getIp(), proxy.getPort(), "http");
  47. RequestConfig config = RequestConfig.custom()
  48. .setProxy(proxyHost)
  49. .build();
  50. request.setConfig(config);
  51. }
  52.  
  53. request.setHeader("User-Agent", USER_AGENT);
  54. request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,*.*");
  55. request.setHeader("Accept-Language", "cs-CZ,cs;q=0.8,en;q=0.6,fr;q=0.4");
  56. if (referal.length() > 0) {
  57. request.setHeader("Referer", referal);
  58. }
  59.  
  60. HttpResponse response = client.execute(request);
  61.  
  62. int responseCode = response.getStatusLine().getStatusCode();
  63.  
  64. if (responseCode == 403) {
  65. throw new Exeption403("403");
  66. }
  67.  
  68. BufferedReader rd = new BufferedReader(
  69. new InputStreamReader(response.getEntity().getContent()));
  70.  
  71. StringBuilder result = new StringBuilder();
  72. String line = "";
  73. while ((line = rd.readLine()) != null) {
  74. result.append(line);
  75. }
  76.  
  77. request.releaseConnection();
  78.  
  79. // set cookies
  80. setCookies(response.getFirstHeader("Set-Cookie") == null ? ""
  81. : response.getFirstHeader("Set-Cookie").toString());
  82.  
  83. return result.toString();
  84. }
  85.  
  86. public String GetPageContent(String url) throws Exception {
  87. return getPageContent(url, null, "");
  88. }
  89.  
  90. public void sendPost(String url, List<NameValuePair> postParams, Proxy proxy) throws IOException, Exeption403 {
  91.  
  92. HttpPost post = new HttpPost(url);
  93. if (proxy != null) {
  94. HttpHost proxyHost = new HttpHost(proxy.getIp(), proxy.getPort(), "http");
  95. RequestConfig config = RequestConfig.custom()
  96. .setProxy(proxyHost)
  97. .build();
  98. post.setConfig(config);
  99. }
  100.  
  101. // add header
  102. post.setHeader("Host", "www.websurf.cz");
  103. post.setHeader("User-Agent", USER_AGENT);
  104. post.setHeader("Accept",
  105. "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  106. post.setHeader("Accept-Language", "cs-CZ,cs;q=0.8,en;q=0.6,fr;q=0.4");
  107. post.setHeader("Cookie", getCookies());
  108. post.setHeader("Connection", "keep-alive");
  109. post.setHeader("Content-Lenght", "29");
  110. post.setHeader("Referer", "https://www.websurf.cz/");
  111. post.setHeader("Content-Type", "application/x-www-form-urlencoded");
  112.  
  113. post.setEntity(new UrlEncodedFormEntity(postParams));
  114.  
  115. HttpResponse response = client.execute(post);
  116.  
  117. int responseCode = response.getStatusLine().getStatusCode();
  118. if (responseCode == 403) {
  119. throw new Exeption403("403");
  120. }
  121.  
  122. // System.out.println("\nSending 'POST' request to URL : " + url);
  123. // System.out.println("Post parameters : " + postParams);
  124. // System.out.println("Response Code : " + responseCode);
  125. BufferedReader rd = new BufferedReader(
  126. new InputStreamReader(response.getEntity().getContent()));
  127.  
  128. StringBuffer result = new StringBuffer();
  129. String line = "";
  130. while ((line = rd.readLine()) != null) {
  131. result.append(line);
  132. }
  133. post.releaseConnection();
  134. // System.out.println(result.toString());
  135. }
  136.  
  137. public void sendPost(String url, List<NameValuePair> postParams)
  138. throws Exception {
  139. sendPost(url, postParams, null);
  140. }
  141.  
  142. private List<NameValuePair> getFormParams(
  143. String html, String username, String password)
  144. throws UnsupportedEncodingException {
  145.  
  146. // System.out.println("Extracting form's data...");
  147. Document doc = Jsoup.parse(html);
  148.  
  149. Element loginform = doc.getElementsByTag("form").get(0);
  150. Elements inputElements = loginform.getElementsByTag("input");
  151.  
  152. List<NameValuePair> paramList = new ArrayList<>();
  153.  
  154. for (Element inputElement : inputElements) {
  155. String key = inputElement.attr("name");
  156. String value = inputElement.attr("value");
  157.  
  158. if (key.equals("jmeno")) {
  159. value = username;
  160. } else if (key.equals("heslo")) {
  161. value = password;
  162. } else if (key.equals("submit")) {
  163. value = "Přihlásit se";
  164. }
  165.  
  166. paramList.add(new BasicNameValuePair(key, value));
  167.  
  168. }
  169.  
  170. return paramList;
  171. }
  172.  
  173. public String getCookies() {
  174. return cookies;
  175. }
  176.  
  177. public void setCookies(String cookies) {
  178. this.cookies = cookies;
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement