Skip to content

Instantly share code, notes, and snippets.

@kkszysiu
Created September 14, 2011 18:16
Show Gist options
  • Save kkszysiu/1217311 to your computer and use it in GitHub Desktop.
Save kkszysiu/1217311 to your computer and use it in GitHub Desktop.
Simple snippet for Onet Czat's authorisation procedure written in Java.
package a;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
public class Test {
protected final static String username = "username"; // Onet Chat's username
protected final static String password = "pwd"; // Onet Chat's password
/**
* @param args
*/
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://kropka.onet.pl/_s/kropka/1?DV=czat/applet/FULL");
System.out.println("executing request " + httpget.getURI());
// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
System.out.println("Step 1 ----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
// Consume response content
EntityUtils.consume(entity);
HttpGet httpget2 = new HttpGet("http://czat.onet.pl/myimg.gif");
System.out.println("executing request " + httpget2.getURI());
// Pass local context as a parameter
HttpResponse response2 = httpclient.execute(httpget2, localContext);
HttpEntity entity2 = response2.getEntity();
System.out.println("Step 2 ----------------------------------------");
System.out.println(response2.getStatusLine());
List<Cookie> cookies2 = cookieStore.getCookies();
for (int i = 0; i < cookies2.size(); i++) {
System.out.println("Local cookie: " + cookies2.get(i));
}
// Consume response content
EntityUtils.consume(entity2);
System.out.println("Step 3 ----------------------------------------");
HttpPost httppost = new HttpPost("http://secure.onet.pl/mlogin.html");
System.out.println("executing request " + httppost.getURI());
try {
//r=&url=&login=%1&haslo=%2&app_id=20&ssl=1&ok=1
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("r", ""));
nameValuePairs.add(new BasicNameValuePair("url", ""));
nameValuePairs.add(new BasicNameValuePair("login", username));
nameValuePairs.add(new BasicNameValuePair("haslo", password));
nameValuePairs.add(new BasicNameValuePair("app_id", "20"));
nameValuePairs.add(new BasicNameValuePair("ssl", "0"));
nameValuePairs.add(new BasicNameValuePair("ok", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response3 = httpclient.execute(httppost, localContext);
HttpEntity entity3 = response3.getEntity();
// Consume response content
EntityUtils.consume(entity3);
} catch (HttpResponseException e) {
//System.err.println(e.response.);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
System.out.println("Step 4 ----------------------------------------");
HttpPost httppost2 = new HttpPost("http://czat.onet.pl/include/ajaxapi.xml.php3");
System.out.println("executing request " + httppost2.getURI());
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("api_function", "getUoKey"));
nameValuePairs.add(new BasicNameValuePair("params", "a:3:{s:4:\"nick\";s:"+username.length()+":\""+username+"\";s:8:\"tempNick\";i:0;s:7:\"version\";s:22:\"1.1(20110818-1158 - R)\";}"));
httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response4 = httpclient.execute(httppost2, localContext);
HttpEntity entity4 = response4.getEntity();
BufferedReader reader = new BufferedReader(new
InputStreamReader(entity4.getContent()));
System.out.println(reader.readLine());
EntityUtils.consume(entity4);
} catch (HttpResponseException e) {
//System.err.println(e.response.);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
public boolean fourthStep() {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment