lijas
lijas

Reputation: 474

knowing what header to send to server

On this website, you can type in your student-card-number, and then it will display how much money is left on the card. Is it possible to this in java, using for example using JSOUP?. How do I know what header I should use etc when sending the card number to the server? When I use chromes developer tool, I get information about the header that looks like this

Remote Address:95.80....
Request URL:http://kortladdning3.chalmerskonferens.se/CardLoad_Order.aspx
Request Method:GET
Status Code:200 OK
Response Headers
view source
Cache-Control:private
Content-Length:6807
etc...

Could I use this information in some way?

So basically what I want is to get how much money that is left, using JAVA.

EDIT:

This is my current code:

res = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36")
                .data("txtCardNumber", "3819xxxxxxxxxx")
                .data("__VIEWSTATE",     "balbalblaonglong")
                .data("__EVENTVALIDATION", "/wEWBAKG7bXPBQLi0uqnCgKF69rWBAK14fOOCgrUt4CBVP4K0VKe0uOPxLSAu26y")
                .data("hiddenIsMobile", "desktop")
                .cookie("ASP.NET_SessionId", "kcvawxel4bzg3yqotr22b1ig")
                .method(Method.GET)
                .execute();

This gives me this error:

HTTP error fetching URL. Status=500, URL=http://kortladdning3.chalmerskonferens.se/CardLoad_Order.aspx?txtCardNumber=3819276248836621&__VIEWSTATE=%2FwEPDwUHNjA4NDA1NQ9kFgQCAw9kFgoCAQ9kFgICAQ8PFgIeBFRleHQFClBUTSBLb3J0bnJkZAICDxYCHgdWaXNpYmxlaGQCAw8WAh8BaGQCBA8WAh8BaGQCBQ8WAh8BaBYCAgEPEGRkFgBkAgUPDxYCHwAFCShkZXNrdG9wKWRkZGzBhwMIv3yxqKnC0C7%2BPlC0PlDG&__EVENTVALIDATION=%2FwEWBAKG7bXPBQLi0uqnCgKF69rWBAK14fOOCgrUt4CBVP4K0VKe0uOPxLSAu26y&hiddenIsMobile=desktop

If i only have .data("txtCardNumber", "3819276248836621"), I dont get a error, but I dont get the correct site (it basically says wrong card-number). Any tips?

EDIT2: It kind of works now. If I add more cookie-information, it will show how much money I have left (Great!). However, if I change the card number (to my friends), I still get the how much money it is left on my own card. I guess this has to something with the cookies? Any tips to add a new cookie session?

EDIT3: Now I get cookies before sending data, but it still does not work. I tried sending the encoded and decoded data, nothing works.

        url = "http://kortladdning3.chalmerskonferens.se/CardLoad_Order.aspx";



        Connection.Response loginForm = Jsoup.connect(url)
                .method(Connection.Method.GET)
                .execute();

        Document document = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5)   AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36")
//                       .data("__VIEWSTATE","%2FwEPDwUHNjA4NDA1NQ9kFgQCAw9kFgoCAQ9kFgICAQ8PFgIeBFRleHQFClBUTSBLb3J0bnJkZAICDxYCHgdWaXNpYmxlaGQCAw8WAh8BaGQCBA8WAh8BaGQCBQ8WAh8BaBYCAgEPEGRkFgBkAgUPDxYCHwAFCShkZXNrdG9wKWRkZGzBhwMIv3yxqKnC0C7%2BPlC0PlDG")
                .data("__EVENTVALIDATION", "%2FwEWBAKG7bXPBQLi0uqnCgKF69rWBAK14fOOCgrUt4CBVP4K0VKe0uOPxLSAu26y")
                .data("hiddenIsMobile", "desktop")
                .data("txtCardNumber", "xxxxxxxxxxxx")
                .data("SavedCardNumber", "")
                .data("btnNext","N%C3%A4sta")
                .cookies(loginForm.cookies())
                .get();

Upvotes: 1

Views: 921

Answers (1)

Joel Min
Joel Min

Reputation: 3457

You can definitely use Jsoup for this. In fact I have used Jsoup to retrieve information of a user from a library website successfully. Anyways I will provide you with the steps I followed to complete this task.

Step 1: The student card number that you enter should be an input of a form. So simply right-click the area where you type in your student number and select Inspect Element. There you will be able to inspect the property of the input and the form.

Step 2: Closely investigate the form and keep record of ALL INPUT FIELDS (including hidden fields). You need to know the name and value of all fields. You also need to know if the form request is made in GET or POST.

Step 3: Now let's get to the fun part. Use the following code snippet to make your request to the server and retrieve the form.

Connection.Response res = Jsoup.connect(url)
                               .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
                               .data(inputName1, inputValue1
                                     inputName2, inputValue2)
                               .cookies(cookies) //do not add this line if no cookies are required
                               .method(Method.GET)
                               .execute();

url is the request address, which in your case would be http://kortladdning3.chalmerskonferens.se/CardLoad_Order.aspx. userAgent tells the server your browser details, data is where you put your name&value pairs of all input fields in the form. cookies is where you put your cookies, you need to check if your request requires cookies to be accepted by the server, this can be checked in the network tab under "cookies" section (if no cookies are required, remove the cookies line). method specifies your request method.

The retrieved res object will contain all the info you need, the html, cookies and everything.

These are just generalised steps. You might need to add more code according to your needs, but basically this is the way you work with requests and servers.

UPDATE

I see that you are passing hardcoded string values of your cookies, which I believe is the reason for your problem. How are you retrieving your cookies? Cookies for your session changes everytime you make a request or login, you should be saving and passing your cookies dynamically.

You can get your cookies by calling res.cookies()

Upvotes: 3

Related Questions