Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.http.HttpEntity;
- import org.apache.http.client.methods.HttpUriRequest;
- import org.apache.http.client.methods.RequestBuilder;
- import org.apache.http.client.protocol.ClientContext;
- import org.apache.http.impl.client.BasicCookieStore;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.protocol.BasicHttpContext;
- import org.apache.http.protocol.HttpContext;
- import org.apache.http.util.EntityUtils;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URI;
- import java.net.URISyntaxException;
- public class Client {
- private DefaultHttpClient client = new DefaultHttpClient();
- private HttpContext context = new BasicHttpContext();
- public Client() throws URISyntaxException, IOException {
- BasicCookieStore store = new BasicCookieStore();
- context.setAttribute(ClientContext.COOKIE_STORE, store);
- }
- public void logIn(String username, String password) throws URISyntaxException, IOException {
- HttpUriRequest login = RequestBuilder.post()
- .setUri(new URI("http://www.derpforum.nl/?p=login"))
- .addParameter("username", username)
- .addParameter("password", password)
- .addHeader("Referer", "http://www.derpforum.nl/")
- .build();
- EntityUtils.consume(client.execute(login, context).getEntity());
- }
- public String getHomePage() throws URISyntaxException, IOException {
- HttpUriRequest get = RequestBuilder.get()
- .setUri(new URI("http://www.derpforum.nl"))
- .build();
- HttpEntity entity = client.execute(get, context).getEntity();
- BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
- StringBuilder builder = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null)
- builder.append(line).append('\n');
- EntityUtils.consume(entity);
- return builder.toString();
- }
- public void CreateTopic(int forumId, String title, String content) throws IOException {
- HttpUriRequest post = RequestBuilder.post()
- .setUri("http://www.derpforum.nl/?p=forum/nieuwtopic&id=")
- .addParameter("forum", String.valueOf(forumId))
- .addParameter("title", title)
- .addParameter("message", content)
- .addHeader("Referer", "http://www.derpforum.nl/?p=forum/nieuwtopic")
- .build();
- EntityUtils.consume(client.execute(post, context).getEntity());
- }
- public void PostComment(int postId, String content) throws IOException {
- HttpUriRequest post = RequestBuilder.post()
- .setUri("http://www.derpforum.nl/?p=forum/topic&id=" + postId)
- .addParameter("Reply", content)
- .addHeader("Referer", "http://www.derpforum.nl/?p=forum/topic&id=" + postId)
- .build();
- EntityUtils.consume(client.execute(post, context).getEntity());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement