Advertisement
Guest User

Client.java

a guest
Aug 19th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import org.apache.http.HttpEntity;
  2. import org.apache.http.client.methods.HttpUriRequest;
  3. import org.apache.http.client.methods.RequestBuilder;
  4. import org.apache.http.client.protocol.ClientContext;
  5. import org.apache.http.impl.client.BasicCookieStore;
  6. import org.apache.http.impl.client.DefaultHttpClient;
  7. import org.apache.http.protocol.BasicHttpContext;
  8. import org.apache.http.protocol.HttpContext;
  9. import org.apache.http.util.EntityUtils;
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.IOException;
  13. import java.io.InputStreamReader;
  14. import java.net.URI;
  15. import java.net.URISyntaxException;
  16.  
  17. public class Client {
  18.  
  19.     private DefaultHttpClient client = new DefaultHttpClient();
  20.     private HttpContext context = new BasicHttpContext();
  21.  
  22.     public Client() throws URISyntaxException, IOException {
  23.         BasicCookieStore store = new BasicCookieStore();
  24.         context.setAttribute(ClientContext.COOKIE_STORE, store);
  25.     }
  26.  
  27.     public void logIn(String username, String password) throws URISyntaxException, IOException {
  28.         HttpUriRequest login = RequestBuilder.post()
  29.                 .setUri(new URI("http://www.derpforum.nl/?p=login"))
  30.                 .addParameter("username", username)
  31.                 .addParameter("password", password)
  32.                 .addHeader("Referer", "http://www.derpforum.nl/")
  33.                 .build();
  34.         EntityUtils.consume(client.execute(login, context).getEntity());
  35.     }
  36.  
  37.     public String getHomePage() throws URISyntaxException, IOException {
  38.         HttpUriRequest get  = RequestBuilder.get()
  39.                 .setUri(new URI("http://www.derpforum.nl"))
  40.                 .build();
  41.         HttpEntity entity = client.execute(get, context).getEntity();
  42.         BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
  43.         StringBuilder builder = new StringBuilder();
  44.         String line;
  45.         while ((line = reader.readLine()) != null)
  46.             builder.append(line).append('\n');
  47.         EntityUtils.consume(entity);
  48.         return builder.toString();
  49.     }
  50.  
  51.     public void CreateTopic(int forumId, String title, String content) throws IOException {
  52.         HttpUriRequest post = RequestBuilder.post()
  53.                 .setUri("http://www.derpforum.nl/?p=forum/nieuwtopic&id=")
  54.                 .addParameter("forum", String.valueOf(forumId))
  55.                 .addParameter("title", title)
  56.                 .addParameter("message", content)
  57.                 .addHeader("Referer", "http://www.derpforum.nl/?p=forum/nieuwtopic")
  58.                 .build();
  59.         EntityUtils.consume(client.execute(post, context).getEntity());
  60.     }
  61.  
  62.     public void PostComment(int postId, String content) throws IOException {
  63.         HttpUriRequest post = RequestBuilder.post()
  64.                 .setUri("http://www.derpforum.nl/?p=forum/topic&id=" + postId)
  65.                 .addParameter("Reply", content)
  66.                 .addHeader("Referer", "http://www.derpforum.nl/?p=forum/topic&id=" + postId)
  67.                 .build();
  68.         EntityUtils.consume(client.execute(post, context).getEntity());
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement