Go Back   Developer Program Forum > Sports API > .NET

.NET Discussion about the .NET Framework (C#/VB), Microsoft's platform for building applications that have visually stunning user experiences, seamless and secure communication, and the ability to model a range of business processes.

Thread Closed
 
Thread Tools Display Modes
  #1  
Old 04-04-2012, 03:51 PM
Regi Keyz's Avatar
Regi Keyz Regi Keyz is offline
Junior Member
 
Join Date: Apr 2012
Location: In a house
Posts: 25
Default A simple Betfair call in C#

Hi,

I am an ex-programmer (several years experience).

I have been betting for a while, and would like to automate my bets.

I started learning C# last week and am comfortable in use of language.

I CANNOT FIND ANY CLEAR REFERENCES ON HOW TO CONNECT TO BETFAIR USING THEIR API.

I read the API doc (a sticky in this forum), and sure I can see the methods, but it does not tell me how to call these methods.. I do not find it useful.
(The example of a bot code sticky isn't working on my Windows 7 machine)

1) Please could someone be able to paste me the full code, to log into Betfair
2) Please could somebody give me code to back 1 football match at a price I chose, it would be a great help.

This would be an amazing start, and probably after this I can figure out the rest..

Does anyone have any links for a simple step-by-step tutorial on how to use the API in C#? So far, everything I've seen seems very cryptic..

Thank-you for taking the time to read this, it's appreciated.
Kindest Regards
Rich
  #2  
Old 04-04-2012, 08:21 PM
wotsisname wotsisname is offline
Senior Member
 
Join Date: Jan 2009
Posts: 219
Default

You've learnt C# in a week? I'm not a .Net programmer but a quick Google throws up plenty...
http://bit.ly/HlF2TW
__________________
http://www.bespokebots.com
  #3  
Old 04-05-2012, 06:55 AM
AlgoTrader AlgoTrader is offline
Senior Member
 
Join Date: Mar 2012
Posts: 513
Default

You need no programming language to learn what BF API is. The betfair API is a *web* service, so you can use any tool that can send HTTPS requests. The browser is good, but I would recomend use curl.exe as it much better tool for sending POST requests.

Make a file login.xml in any text editor and put the contents:
Code:
C:\Temp>type login.xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XM
LSchema">
  <soap:Body>
    <login xmlns="http://www.betfair.com/publicapi/v3/BFGlobalService/">
      <request>
        <locationId>0</locationId>
        <password>bfpassword</password>
        <productId>82</productId>
        <username>bfusername</username>
        <vendorSoftwareId>0</vendorSoftwareId>
      </request>
    </login>
  </soap:Body>
</soap:Envelope>
Download curl for windows and type from the command line:

Code:
C:\Temp>curl -d @login.xml -H "SOAPAction: login" https://api.betfair.com/global/v3/BFGlobalService
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:n2="http://www.betfair.com/pub
licapi/types/global/v3/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><n:loginResponse xmlns:n="http://www.betfair.com/publicapi/v3/BFGlobalService/"
><n:Result xsi:type="n2:LoginResp"><header xsi:type="n2:APIResponseHeader"><errorCode xsi:type="n2:APIErrorEnum">OK</errorCode><minorErrorCode xsi:nil="1"></min
orErrorCode><sessionToken xsi:nil="1"></sessionToken><timestamp xsi:type="xsd:dateTime">2012-04-05T06:43:07.583Z</timestamp></header><currency xsi:nil="1"></cur
rency><errorCode xsi:type="n2:LoginErrorEnum">INVALID_USERNAME_OR_PASSWORD</errorCode><minorErrorCode xsi:nil="1"></minorErrorCode><validUntil xsi:type="xsd:da
eTime">0001-01-01T00:00:00.000Z</validUntil></n:Result></n:loginResponse></soap:Body></soap:Envelope>
That's all! We have sent the request and got the answer. After formatting, the XML looks like
Code:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:n2="http://www.betfair.com/publicapi/types/global/v3/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <n:loginResponse xmlns:n="http://www.betfair.com/publicapi/v3/BFGlobalService/">
      <n:Result xsi:type="n2:LoginResp">
        <header xsi:type="n2:APIResponseHeader">
          <errorCode xsi:type="n2:APIErrorEnum">OK</errorCode>
          <minorErrorCode xsi:nil="1"></minorErrorCode>
          <sessionToken xsi:nil="1"></sessionToken>
          <timestamp xsi:type="xsd:dateTime">2012-04-05T06:43:07.583Z</timestamp>
        </header>
        <currency xsi:nil="1"></currency>
        <errorCode xsi:type="n2:LoginErrorEnum">INVALID_USERNAME_OR_PASSWORD</errorCode>
        <minorErrorCode xsi:nil="1"></minorErrorCode>
        <validUntil xsi:type="xsd:dateTime">0001-01-01T00:00:00.000Z</validUntil>
      </n:Result>
    </n:loginResponse>
  </soap:Body>
</soap:Envelope>
Can you send HTTPS requests from C# apps? If not, then this way is not for you.

I would recommend you to use the other way - find the Betfair API for some language (C# is nice, but there are better choices for inexperienced programmers) and use it. It will save you from SOAP, HTTPS Anyway, nobody will provide you the complete example, try to use own brains.

I strongly disagree Betfair documentations is bad. It says what you should send, where you should send and what you should get and privides quite good examples

Last edited by AlgoTrader; 04-05-2012 at 07:09 AM
  #4  
Old 04-06-2012, 09:52 AM
Regi Keyz's Avatar
Regi Keyz Regi Keyz is offline
Junior Member
 
Join Date: Apr 2012
Location: In a house
Posts: 25
Default Message to 'wotisname'

"wotsisname You've learnt C# in a week?"

Yes mate, I coded computer games in the 90's using mostly C++, then in next job did several Java certifications.. it's all the same stuff really, very similar just slightly different syntax..

"I'm not a .Net programmer but a quick Google throws up plenty...
http://bit.ly/HlF2TW"


haha, yeah thanks for that Einstein! It didn't answer my question..! Half the links that came up on that page I'd already looked at and the other half were useless.. ! If you read my question, you will notice that I was looking for full code examples.. you can go back to sleep now mate

Last edited by Regi Keyz; 04-06-2012 at 10:09 AM
  #5  
Old 04-06-2012, 10:08 AM
Regi Keyz's Avatar
Regi Keyz Regi Keyz is offline
Junior Member
 
Join Date: Apr 2012
Location: In a house
Posts: 25
Default Message to 'AlgoTrader'

Thank-you for your more useful reply..

"Can you send HTTPS requests from C# apps? If not, then this way is not for you. "

I never did much web programming, it wasn't around so much in my day..
Suffice to say, I've never sent/received HTTPS requests..

I've just been looking at the WebRequest class in C#.. perhaps this is where I look..


"I would recommend you to use the other way - find the Betfair API for some language (C# is nice, but there are better choices for inexperienced programmers) and use it. It will save you from SOAP, HTTPS Anyway, nobody will provide you the complete example, try to use own brains."

Yeah, I've never used SOAP or WSDL .. all I need to do is get one small example fully working then the rest I can figure out..I'm dyslexic so I usually struggle initially with new learning curves because everything appears 'messy/confusing' and I find it hard to see the big picture.. but with a lot of effort, I can quickly get up to speed.

I'm still trying to get an overview of what I must do. From what I understand, I can use WSDL on my C# code (somehow), and then call the Betfair API methods (that automatically send XML to Betfair server).. is that correct?

I'd rather use C# for now rather than another language, because Im also coding financial trading systems, it will reduce the learning curve of learning two new languages.. or in your opinion is using eg Python or Perl much much easier? eg do I have to still send XML or are there functions readily available to do that workload for me?


"I strongly disagree Betfair documentations is bad. It says what you should send, where you should send and what you should get and privides quite good examples"
Haha.. all I saw was pages and pages of XML and I thought 'how the **** does this help me get started.. ' I've since found this 'http://fx.cz/sklad/betfairapi.pdf' and that is more useful.. tho Im still struggling on how to have a full C# programme that will log me in..

Any chance someone could copy and paste some fully working .cs code from their existing code that will just connect me to Betfair login and place one bet? One I have this structure, I will very easily be able to use the api to do all the other functions Im sure..

Thank-you Kindly

Last edited by Regi Keyz; 04-06-2012 at 10:12 AM
  #6  
Old 04-06-2012, 10:53 AM
AlgoTrader AlgoTrader is offline
Senior Member
 
Join Date: Mar 2012
Posts: 513
Default

Quote:
Originally Posted by Regi Keyz View Post
Any chance someone could copy and paste some fully working .cs code from their existing code that will just connect me to Betfair login and place one bet?
Do you really think someone should think instead of you?

Placing a single bet is not that easy.
1) invoke login to get a session key
2) invoke a getAllMarkets to find marketId of interesting market
3) invoke a getMarket to find selectionId of player you want back/lay
4) invoke a placeBet to place Bet

There is JavaScript app that do everything I described:
https://github.com/BetfairTrader/bet...cancel_bets.js

I do not use C# as it bound to Windows, I plan to use cheap Linux hosting in UK.
  #7  
Old 04-27-2012, 10:54 PM
Regi Keyz's Avatar
Regi Keyz Regi Keyz is offline
Junior Member
 
Join Date: Apr 2012
Location: In a house
Posts: 25
Default

>>Do you really think someone should think instead of you?

What?! Why would you say this? It's not about me not thinking! Mate, this is a forum - a programming support forum - asking for sample code, where another 1000 others can also benefit from it, is true to the whole purpose of having a forum, it's kind of what people do in programming forums..

And yes, I do think someone should stick code on here..! I'm not asking someone to think instead of me.. to write my bot will take me 100's of hours..I'm not expecting anyone to do that.. I just wanted a simple C# login to start me off on the right track - as I said in a previous post, the rest I can figure out..!!

An experienced programmer should be able to copy&paste the code for my request in 5 mins.. and help out a whole lode of other beginners - it's really not such a big ask mate, is it?!


>>There is JavaScript app that do everything I described:
>>https://github.com/BetfairTrader/bet...cancel_bets.js

Mate, you first accuse me of asking someone to think instead of myself..
but then you actually give me what I was asking for.. just not exactly..coz it was in a completely different language.. I just wanted someone to do this in C# mate.. this is a .NET forum. Your Javascript was nice to look at, but not so much use.

Look, in the weeks that I asked this, it's been a bit of a ballache, and taken alot of time, going down blind alleys but now I've figured out how to do this..

Just it would have been a much much easier, if someone had taken 5 mins to copy & paste some of their code.. I could have achieved my result in less than half the time easily.

I'm working full out on my bot now, but once thats done, I intend to leave a nice clear post in this forum, that explains to a beginner, how to start programming in C#, with BF API.. it will save them much of the drama I went through.. and make this support forum even more useful for future beginners.


End-ex

Last edited by Regi Keyz; 04-27-2012 at 10:57 PM
  #8  
Old 04-28-2012, 12:33 AM
MrChameleon MrChameleon is offline
Member
 
Join Date: Apr 2009
Posts: 36
Default

At the risk of copping it as some previous posters did, here is a class with login, logout methods. I haven't used it for a while, I now use ruby implementation instead.

I can post more stuff, prices, place, cancel etc if you like.

Use at own risk.

Hth

Chris

Code:
public class BetfairApiAusUk {
        const String UrlUk = "https://api.betfair.com/exchange/v5/BFExchangeService";
        const String UrlAus = "https://api-au.betfair.com/exchange/v5/BFExchangeService";

        BetFairGlobal.BFGlobalService globalService;
        BetFairGlobal.APIRequestHeader globalRequestHeader;
        //
        BetFairAusUk.BFExchangeService localService;
        BetFairAusUk.APIRequestHeader localRequestHeader;
        //
        String sessionToken;
        BetfairDbi dbi;

        public BetfairApiAusUk(Service service) {
            globalService = new BetFairGlobal.BFGlobalService();
            globalRequestHeader = new BetFairGlobal.APIRequestHeader();
            //
            String url;
            if (service == Service.AUS) { url = UrlAus; }
            else { url = UrlUk; }
            localService = new BetFairAusUk.BFExchangeService() { Url = url };
            localRequestHeader = new BetFairAusUk.APIRequestHeader();
            //
            dbi = new BetfairDbi();
        }

        public bool Login() {
            bool retVal = false;
            String userName = BetfairInfo.Instance.UserName;
            String userPass = BetfairInfo.Instance.UserPass;
            BetFairGlobal.LoginReq request = new BetFairGlobal.LoginReq();
            try {
                BetFairGlobal.LoginResp response = new BetFairGlobal.LoginResp();
                request.username = userName;
                request.password = userPass;
                request.productId = 82; // Free API.
                response = globalService.login(request);
                if (CheckHeader(response.header) == BetFairGlobal.APIErrorEnum.OK) {
                    sessionToken = response.header.sessionToken;
                    retVal = true;
                }
            }
            catch (System.Net.WebException) {
                retVal = false;
            }
            return retVal;
        }

        public bool KeepAlive() {
            bool retVal = false;
            BetFairGlobal.KeepAliveReq request = new BetFairGlobal.KeepAliveReq();
            request.header = GlobalRequestHeader;
            BetFairGlobal.KeepAliveResp response = globalService.keepAlive(request);
            if (CheckHeader(response.header) == BetFairGlobal.APIErrorEnum.OK) {
                sessionToken = response.header.sessionToken;
                retVal = true;
            }
            return retVal;
        }

        public bool Logout() {
            bool retVal = false;
            BetFairGlobal.LogoutReq request = new BetFairGlobal.LogoutReq();
            request.header = GlobalRequestHeader;
            BetFairGlobal.LogoutResp response = globalService.logout(request);
            if (CheckHeader(response.header) == BetFairGlobal.APIErrorEnum.OK) {
                sessionToken = response.header.sessionToken;
                retVal = true;
            }
            return retVal;
        }

        //etc etc
  #9  
Old 04-28-2012, 04:44 AM
AlgoTrader AlgoTrader is offline
Senior Member
 
Join Date: Mar 2012
Posts: 513
Default

Quote:
Originally Posted by Regi Keyz View Post
I'm working full out on my bot now, but once thats done, I intend to leave a nice clear post in this forum, that explains to a beginner, how to start programming in C#, with BF API.. it will save them much of the drama I went through.. and make this support forum even more useful for future beginners.
You are not the first who experienced the drama of steep learning curve using BF API. You have to go your own Zen path of BF programming.

There are reasons why C#.Net is certainly not the best choice.
1) tough bind to windows, no way to switch to cheap VPS on linux
2) heavy and slow .Net CLI runtime, big memory requirements
3) too static language, too much of sex of too verbose programming
4) no async things, threads with all the synchronization stuff

It was the language of choice 10 years ago as it is a step forward comparing to C++, but not now. JavaScript is much easier, is trully cross-platform, amazingly fast and resource economical.
  #10  
Old 05-06-2012, 02:43 AM
Regi Keyz's Avatar
Regi Keyz Regi Keyz is offline
Junior Member
 
Join Date: Apr 2012
Location: In a house
Posts: 25
Default

Hi MrChameleon

Thank-you for that, I've actually got code working now.. though it was nice to see your Keep Alive method, as that's why I am going to add next.. if I leave my code too long I get runtime error as session key changes..!

Thank-you for your offer of help, this was what I was originally looking for
Thread Closed

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


All times are GMT. The time now is 11:51 AM.


BETFAIR® and the BETFAIR LOGO are registered trade marks of The Sporting Exchange Limited. Data on Betfair website(s) (including pricing data) is protected by © and database rights. It may not be used for any purpose without a licence. © The Sporting Exchange Limited. All rights reserved.