Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:25
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save peanutwolf/415ea930721a15220ecd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.Header; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.CookieStore; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.config.CookieSpecs; | |
import org.apache.http.client.config.RequestConfig; | |
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.params.ClientPNames; | |
import org.apache.http.client.params.CookiePolicy; | |
import org.apache.http.config.Registry; | |
import org.apache.http.config.RegistryBuilder; | |
import org.apache.http.conn.util.PublicSuffixMatcher; | |
import org.apache.http.conn.util.PublicSuffixMatcherLoader; | |
import org.apache.http.cookie.*; | |
import org.apache.http.cookie.params.CookieSpecPNames; | |
import org.apache.http.impl.client.BasicCookieStore; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.impl.cookie.*; | |
import org.apache.http.message.BasicHeader; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.params.BasicHttpParams; | |
import org.apache.http.protocol.HttpContext; | |
import org.apache.http.util.EntityUtils; | |
import org.apache.http.util.TextUtils; | |
import java.io.BufferedReader; | |
import java.io.Console; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.MalformedURLException; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Scanner; | |
import java.util.regex.Pattern; | |
public class VKapi { | |
HttpResponse response; | |
HttpEntity entity; | |
HttpPost post; | |
HttpGet get; | |
ArrayList<BasicNameValuePair> postParameters; | |
Scanner userInput = new Scanner(System.in); | |
String access_token; | |
private String client_id = "5007256"; | |
private String scope = "messages"; | |
private String redirect_uri = "http://oauth.vk.com/blank.html"; | |
private String display = "popup"; | |
private String response_type = "token"; | |
private String email = "*********"; | |
private String pass = "**********"; | |
String to_h = "aHR0cDovL29hdXRoLnZrLmNvbS9hdXRob3JpemU/Y2xpZW50X2lkPTUwMDcyNTYmcmVkaXJlY3RfdXJpPWh0dHAlM0ElMkYlMkZvYXV0aC52ay5jb20lMkZibGFuay5odG1sJnJlc3BvbnNlX3R5cGU9dG9rZW4mc2NvcGU9NDA5NiZ2PSZzdGF0ZT0mZGlzcGxheT1wb3B1cA--"; | |
String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"; | |
String accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; | |
String acceptEncoding = "gzip, deflate, sdch"; | |
String acceptLanguage = "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4"; | |
String cacheControl = "max-age=0"; | |
String connection = "keep-alive"; | |
public String setConnection() throws IOException, URISyntaxException { | |
CookieStore cookieStore = new BasicCookieStore(); | |
userInput.useDelimiter(Pattern.compile("\n")); | |
Registry<CookieSpecProvider> r = RegistryBuilder.<CookieSpecProvider>create() | |
.register("myCookieSpec", new MyLenientCookieSpec()) | |
.build(); | |
RequestConfig requestConfig = RequestConfig.custom() | |
.setCookieSpec("myCookieSpec") | |
.build(); | |
CloseableHttpClient httpClient = HttpClients.custom(). | |
setDefaultCookieSpecRegistry(r). | |
setDefaultRequestConfig(requestConfig). | |
setDefaultCookieStore(cookieStore).build(); | |
postParameters = new ArrayList<>(); | |
postParameters.add(new BasicNameValuePair("client_id", client_id)); | |
postParameters.add(new BasicNameValuePair("scope", scope)); | |
postParameters.add(new BasicNameValuePair("redirect_uri", redirect_uri)); | |
postParameters.add(new BasicNameValuePair("display", display)); | |
postParameters.add(new BasicNameValuePair("response_type", response_type)); | |
get = new HttpGet("http://oauth.vk.com/authorize" + | |
"?client_id=" + client_id + | |
"&scope=" + scope + | |
"&redirect_uri=" + redirect_uri + | |
"&display=" + display + | |
"&response_type=" + response_type); | |
response = httpClient.execute(get); | |
get.abort(); | |
if(response == null || response.getStatusLine().getStatusCode() != 200){ | |
return null; | |
} | |
//TODO : Parse HTML for POST request | |
entity = response.getEntity(); | |
System.out.println(EntityUtils.toString(entity)); | |
postParameters = new ArrayList<>(); | |
postParameters.add(new BasicNameValuePair("ip_h", "bb72ffc6761c50979a")); | |
postParameters.add(new BasicNameValuePair("lg_h", userInput.nextLine())); | |
postParameters.add(new BasicNameValuePair("_origin", "http://oauth.vk.com")); | |
// postParameters.add(new BasicNameValuePair("lg_h", "b6827ed0526ef6b806")); | |
postParameters.add(new BasicNameValuePair("to", to_h)); | |
postParameters.add(new BasicNameValuePair("expire", "0")); | |
postParameters.add(new BasicNameValuePair("email", email)); | |
postParameters.add(new BasicNameValuePair("pass", pass)); | |
post = new HttpPost("https://login.vk.com/?act=login&soft=1&utf8=1"); | |
// post.setHeader("Accept", acceptEncoding); | |
// post.setHeader("Accept-Encoding", acceptEncoding); | |
// post.setHeader("Accept-Language", acceptLanguage); | |
// post.setHeader("Cache-Control", cacheControl); | |
// post.setHeader("Connection", connection); | |
// post.setHeader("User-Agent", userAgent); | |
post.setEntity(new UrlEncodedFormEntity(postParameters)); | |
response = httpClient.execute(post); | |
post.abort(); | |
if(response == null || response.getStatusLine().getStatusCode() != 302){ | |
return null; | |
} | |
// post.setHeader("Accept", acceptEncoding); | |
// post.setHeader("Accept-Encoding", acceptEncoding); | |
// post.setHeader("Accept-Language", acceptLanguage); | |
// post.setHeader("Cache-Control", cacheControl); | |
// post.setHeader("Connection", connection); | |
// post.setHeader("User-Agent", userAgent); | |
post = new HttpPost(response.getFirstHeader("location").getValue()); | |
response = httpClient.execute(post); | |
post.abort(); | |
if(response == null || response.getStatusLine().getStatusCode() != 302){ | |
return null; | |
} | |
// post.setHeader("Accept", acceptEncoding); | |
// post.setHeader("Accept-Encoding", acceptEncoding); | |
// post.setHeader("Accept-Language", acceptLanguage); | |
// post.setHeader("Cache-Control", cacheControl); | |
// post.setHeader("Connection", connection); | |
// post.setHeader("User-Agent", userAgent); | |
post = new HttpPost(response.getFirstHeader("location").getValue()); | |
response = httpClient.execute(post); | |
post.abort(); | |
if(response == null || response.getStatusLine().getStatusCode() != 302){ | |
return null; | |
} | |
// post.setHeader("Accept", acceptEncoding); | |
// post.setHeader("Accept-Encoding", acceptEncoding); | |
// post.setHeader("Accept-Language", acceptLanguage); | |
// post.setHeader("Cache-Control", cacheControl); | |
// post.setHeader("Connection", connection); | |
// post.setHeader("User-Agent", userAgent); | |
HttpGet httpGet = new HttpGet(response.getFirstHeader("location").getValue()); | |
response = httpClient.execute(httpGet); | |
httpGet.abort(); | |
if(response == null || response.getStatusLine().getStatusCode() != 200){ | |
return null; | |
} | |
entity = response.getEntity(); | |
System.out.println(EntityUtils.toString(entity)); | |
return ""; | |
} | |
public String getNewMessage() throws ClientProtocolException, IOException, NoSuchAlgorithmException, URISyntaxException { | |
//��������� ������ ������� | |
String url = "https://api.vk.com/method/" + | |
"messages.get" + | |
"?out=0" + | |
"&access_token=" + access_token; | |
String line = ""; | |
try { | |
URL url2 = new URL(url); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(url2.openStream())); | |
line = reader.readLine(); | |
reader.close(); | |
} catch (MalformedURLException e) { | |
} | |
return line; | |
} | |
public static void main(String [] args){ | |
VKapi vKapi = new VKapi(); | |
try { | |
vKapi.setConnection(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class MyLenientCookieSpec implements CookieSpecProvider{ | |
String [] customExpiresPattern = {"EEE, dd MMM yyyy HH:mm:ss z"}; | |
@Override | |
public CookieSpec create(HttpContext httpContext) { | |
return new DefaultCookieSpec(customExpiresPattern, false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment