Sunday, January 11, 2009

MATLAB can get binary data and set Cookies!!!

After an overnight hopeless wandering around with this mad quest in mind i finally managed to read binary data correctly via http connection AND get Cookie at the same time!!! Moreover i can post some data with Cookie via http as well.

Please, if you don't care for Cookie - just DO use urlread!!! I DO mean it!

The only working way was to use Ice(Java) functions in Matlab, which are NOT documented in any way. Therefore one needs to experiment a lot with them!

So if you are not scared yet, here we go:

To read binary data use:

uint8( DataInputStream.read() )


To get cookie use:

CookieString = char(HttpURLConnection.getCookieManager().elements.nextElement.Cookie.toString)

or the following might also work

CookieString = char(HttpURLConnection.getCookieManager().getHeaderField('Set-Cookie') )


To set cookie use:

HttpURLConnection.setRequestProperty('Cookie', CookieString )



Since i am by no means an expert neither in Java nor in Matlab i might have overlooked something. Please do let me know if you know a better way.

ps: of course you start as follows:

URL = java.net.URL('http://example.url');
HttpURLConnection = URL.openConnection();

2 comments:

  1. Hi,

    I tried some of your code in a Matlab command window, and I think a bunch of stuff needs to be set for this to work. Do you have the full program available?

    Yours,

    Raj

    >> CookieString = char(HttpURLConnection.getCookieManager().elements.nextElement.Cookie.toString)
    ??? Undefined variable "HttpURLConnection" or class
    "HttpURLConnection.getCookieManager".

    >>

    ReplyDelete
  2. thanks, i added a postscriptum about how to get that HttpURLConnection.

    If there will be a confusion with a type name - just rename that variable 'HttpURLConnection' into something else and use it afterwards.

    ReplyDelete