Betfair Developers


Java
Discussions about Java. Java technology's versatility, efficiency, platform portability, and security make it the ideal technology for network computing. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

Post Reply
trader174. trader174. is offline
Junior Member
 
Default Login and KeepAlive Examples

Hi!
I am a not professional programmer.
I use apiNG example for building my bot.
I can't understand, how i can login through my bot, and how i can create keep alive request.

Please help me!
P.S. I use Netbeans
trader174. trader174. is offline
Junior Member
 
Default

I found a solution in another post:

package com.XXX.XXXX;

import java.net.*;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;


public class Authentication
{
private static String loginTarget = "https://identitysso.betfair.com/api/login";
private static String keepAliveTarget = "https://identitysso.betfair.com/api/keepAlive";
private static String logoutTarget = "https://identitysso.betfair.com/api/logout";


public Authentication()
{
;
}

public static String login(String username, String password)
{
String token = "PROBLEM";

String s = "";

try
{
s = "&username=" + URLEncoder.encode(username, "UTF-8");
s+= "&password=" + URLEncoder.encode(password, "UTF-8");
s+= "&login=" + URLEncoder.encode("true", "UTF-8");
s+= "&redirectMethod=" + URLEncoder.encode("POST", "UTF-8");
s+= "&product=" + URLEncoder.encode("home.betfair.int", "UTF-8");
s+= "&url=" + URLEncoder.encode("https://www.betfair.com/", "UTF-8");
}
catch(Exception ex)
{
ex.printStackTrace();
}

if(!s.equals(""))
token = makeCall(0, loginTarget, s);

return token;
}

public static String keepAlive()
{
String token = "PROBLEM";
String s = "";

try
{
s = "&" + "product=" + URLEncoder.encode(YOUR_APPKEY, "UTF-8");
s+= "&url=" + URLEncoder.encode("https://www.betfair.com/", "UTF-8");
}

catch(Exception ex)
{
ex.printStackTrace();
}

if(!s.equals(""))
token = makeCall(1, keepAliveTarget, s);

return token;
}

public static String logout()
{
String token = "PROBLEM";
String s = "";

try
{
s = "&" + "product=" + URLEncoder.encode(YOUR_APPKEY, "UTF-8");
s+= "&url=" + URLEncoder.encode("https://www.betfair.com/", "UTF-8");
}

catch(Exception ex)
{
ex.printStackTrace();
}

if(!s.equals(""))
token = makeCall(2, logoutTarget, s);

return token;
}

private static String makeCall(int call, String target, String query)
{
URL url = null;
HttpsURLConnection conn = null;
DataOutputStream outStream = null;
String outString = "PROBLEM";

try
{
url = new URL(target);
conn = (HttpsURLConnection)url.openConnection();

conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setFollowRedirects( true );

if(conn != null)
{
outStream = new DataOutputStream(conn.getOutputStream());

outStream.writeBytes(query);
outStream.flush();
outStream.close();
conn.disconnect();

int responseCode = conn.getResponseCode();

if(call == 0)
{
if(responseCode == 200)
{
String headerName = null;

for (int i = 1; (headerName = conn.getHeaderFieldKey(i))!=null; i++)
{
if (headerName.equals("Set-Cookie"))
{
String cookie = conn.getHeaderField(i);

if(cookie.indexOf("ssoid") > -1)
outString = cookie.substring(cookie.indexOf("=") + 1, cookie.indexOf(";"));
}
}
}
}

else if(responseCode == 200)
outString = "" + responseCode;
}//if(conn != null)
}

catch(Exception ex)
{
ex.printStackTrace();
}

return outString;
}
}
Post Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump