Ребят нужна помощь. Делаю бота для одной онлайн игры и есть проблема с авторизацией. Сделать мне это нужно на java, а знаю я её плохо в отличии от php.
Суть проблемы в чем. Написал туже авторизацию на php и все норм. Вот код:
<?php
class ml{
var $auth_url = "http://mlgame.ru/auth";
var $salt_url = "http://mlgame.ru/salt";
var $game_url = "http://s30.mlgame.ru/";
var $password;
var $login;
function __construct(){}
function curl_get($host, $referer = null){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, "Opera/14.00 (Windows NT 5.1; U; ru) Presto/2.9.168 Version/11.51");
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt'); // сохранять куки в файл
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
$html = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
return $html;
}
function get_salt(){
$salt = json_decode($this->curl_get($this->salt_url), TRUE);
return $salt['salt'];
}
function get_password(){
//MD5( MD5(clearText) + salt );
return md5(md5($this->password)."".$this->get_salt());
}
function auth(){
$to = $this->auth_url."?username=".$this->login."&password=".$this->get_password()."&version=web67894&autologin=false&captcha=null";
$auth = $this->curl_get($to, $this->game_url);
return $auth;
}
}
if(isset($_POST))
{
$ml = new ml();
$ml->login = 'dambenok68';
$ml->password = 'admin2501';
}
//echo $ml->auth();
?>
<?php
$json = $ml->auth();
$obj = json_decode($json);
$result = $obj->{'result'};
if($result == 'ok')
{
echo 'Авторизация успешна<br>Salt: ';
}
else
{
echo 'Неверный логин/пароль ';
}
?>
Пишу тоже самое на java но авторизация не проходит. Код:
package mlgame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class auth {
private static final String USER_AGENT = "Opera/14.00 (Windows NT 5.1; U; ru) Presto/2.9.168 Version/11.51";
private static final String AUTH_URL = "http://mlgame.ru/auth";
private static final String SALT_URL = "http://mlgame.ru/salt";
private static String cookies;
private static HttpClient client = HttpClientBuilder.create().build();
private static String get_salt() {
HttpGet request = new HttpGet(SALT_URL);
request.addHeader("User-Agent", USER_AGENT);
try {
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
final JSONObject obj = new JSONObject(rd.readLine());
return obj.getString("salt");
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
private static String encrypt(String password) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
public static Boolean login(String user, String pass) {
try {
auth http = new auth();
String password = encrypt(encrypt(pass + get_salt()));
CookieHandler.setDefault(new CookieManager());
List<NameValuePair> params = new ArrayList<NameValuePair>(5);
params.add(new BasicNameValuePair("username", user));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("version", "web67894"));
params.add(new BasicNameValuePair("autologin", "false"));
params.add(new BasicNameValuePair("captcha", "null"));
HttpPost post = new HttpPost(AUTH_URL);
post.setHeader("Host", "mlgame.ru");
post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.setHeader("Accept-Language", "en-US,en;q=0.5");
post.setHeader("Cookie", getCookies());
post.setHeader("Connection", "keep-alive");
post.setHeader("Referer", AUTH_URL);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = client.execute(post);
setCookies(response.getFirstHeader("Set-Cookie") == null ? ""
: response.getFirstHeader("Set-Cookie").toString());
int responseCode = response.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
//System.out.println(result.toString());
final JSONObject obj = new JSONObject(result.toString());
if(obj.getString("result") == "error" && obj.getString("errorText") == "wrong account")
{
System.out.println("Login failed");
return false;
}else{
return true;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (ClientProtocolException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (JSONException e) {
e.printStackTrace();
return false;
}
}
public static String getCookies() {
return cookies;
}
public static void setCookies(String cookies) {
cookies = cookies;
}
}