Android Volley Lib connect to Windows IIS

Volley to Windows IIS server? Wtf, this is the last thing I wanna handle with. Cause Windows IIS server usually opens that EVIL “Session Lock” for every connection client. That means, when you try to connect to IIS again and get data, you need to be in the session(tell the server you’re the same guy who got data last time) then, IIS will say: OK! You look like the same guy who got data from me last time. You’re passed. Or, you usually feel….

Fuck that…

So, let me descript a little bit what session is. For ASP.NET, the session will be connection level object which means it’s based on Browser instance. When you use chrome to connect to IIS server, server side will create a Session ID(it looks like anf4vuup3xiq0arjlqla2l55) for this chrome browser to identify who you are. Generally, it will be alive about 20-30 mins, depends on server side setting. This session will be an instance implemented ICollection/IEnumerate. Here is an example:

Session ID Example

You can see the Cookie item which includes Session ID: ASP.NET_SessionId=br5gdriiiaywu1wzublj5e4p, that’s the unique id for your browser.

So, we already know what session is. Now we need to know how to get that session and keep it as browser did for us.

I use volley library as an example, but basically, every 3rd party network library allows us to create a customized Request for sending to the server. So, you can do that in your own library.

protected Response<T> parseNetworkResponse(
        NetworkResponse response) {
    try {
        String json = new String(,
        return Response.success(gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
    // handle errors

Most requests have ready-to-use implementations in the toolbox; if your response is a string, image, or JSON, you probably won’t need to implement a custom Request. For cases where you do need to implement a custom request, this is all you need to do:

  • Extend the Request class, where represents the type of parsed response the request expects. So if your parsed response is a string, for example, create your custom request by extending Request. See the Volley toolbox classes StringRequest and ImageRequest for examples of extending Request.
  • Implement the abstract methods parseNetworkResponse() and deliverResponse(), described in more detail below.

Fortunately, Volley creates a StringRequest for us, all we need to do is extend that class, and modify the parseNetworkResponse function.

Map<String, String> responseHeaders = response.headers;

String sessionString = responseHeaders.get("Set-Cookie");
int index1 = sessionString.indexOf("ASP.NET_SessionId=");
int index2 = sessionString.indexOf("; path");

sessionId = sessionString.substring(index1, index2);

String parsed;
    try {
        parsed = new String(, HttpHeaderParser.parseCharset(response.headers));
    } catch (UnsupportedEncodingException e) {
        parsed = new String(;
    return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));

What I did is to get the responseHeaders out, and extract out the SessionID, and set it as a global variable cause we need it later for sending as the header. Now let’s put SessionId in the header and send it out.

public Map<String, String> getHeaders() throws AuthFailureError {

    if (sessionId == null) {
        return super.getHeaders();
    else {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Cookie", sessionId);
        return headers;

Pretty easy, just put it into getHeaders function when you send out a request. Volley will call getHeaders to see if you wanna send something else to server. So, from now on, you will be the same guy that server knows you.


Too easy, bye IIS.


Happy coding!


One thought on “Android Volley Lib connect to Windows IIS

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s