知道发送到服务器的标题 [英] knowing what header to send to server

查看:118
本文介绍了知道发送到服务器的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个网站上,您可以输入您的学生卡号码,然后它会显示卡上留下了很多钱。在java中可以这样做,例如使用JSOUP?。在将卡号发送到服务器时,如何知道我应该使用什么标题等?当我使用chromes开发者工具时,我得到了有关这个头像的信息。

b $ b请求URL:http://kortladdning3.chalmerskonferens.se/CardLoad_Order.aspx
请求方法:GET
状态码:200 OK
响应标题
查看源
Cache-Control:private
Content-Length:6807
等...



我能否以某种方式使用这些信息?



因此,基本上我想要的是使用JAVA获得剩下的钱。

编辑:



这是我目前的代码:

  res = Jsoup.connect(url)
.userAgent(Mozilla / 5.0(Macintosh; Intel Mac OS X 10_10_5)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 44.0.2403.157 Safari / 537.36)
.data(txtCardNumber,3819xxxxxxxxxx)
.data(__ VIEWSTATE,balbalblaonglong)
.data(__ EVENTVALIDATION,/ wEWBAKG7bXPBQLi0uqnCgKF69rWBAK14fOOCgrUt4CBVP4K0VKe0uOPxLSAu26y)
。数据(hiddenIsMobile,桌面)
.cookie(ASP.NET_SessionId,kcvawxel4bzg3yqotr22b1ig)
.method(Method.GET)
.execute();

这给了我这个错误:

 获取URL的HTTP错误。状态= 500,URL = HTTP://kortladdning3.chalmerskonferens.se/CardLoad_Order.aspx txtCardNumber = 3819276248836621&安培; __VIEWSTATE =%2FwEPDwUHNjA4NDA1NQ9kFgQCAw9kFgoCAQ9kFgICAQ8PFgIeBFRleHQFClBUTSBLb3J0bnJkZAIC​​DxYCHgdWaXNpYmxlaGQCAw8WAh8BaGQCBA8WAh8BaGQCBQ8WAh8BaBYCAgEPEGRkFgBkAgUPDxYCHwAFCShkZXNrdG9wKWRkZGzBhwMIv3yxqKnC0C7%2BPlC0PlDG&安培; __EVENTVALIDATION =%2FwEWBAKG7bXPBQLi0uqnCgKF69rWBAK14fOOCgrUt4CBVP4K0VKe0uOPxLSAu26y&安培; hiddenIsMobile =桌面

如果我只有 .data(txtCardNumber,3819276248836621),我没有一个错误,但我没有得到正确的网站(它基本上说错了卡号)。任何提示?



编辑2:
现在有些作品。如果我添加更多的cookie信息,它会显示我剩下多少钱(太棒了!)。但是,如果我更改卡号(给我的朋友),我仍然可以获得自己卡上的剩余金额。我想这与cookie有关?任何提示添加一个新的cookie会话?



编辑3:
现在我发送数据之前得到cookie,但它仍然没有工作。我试图发送编码和解码的数据,没有任何工作。

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



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

Apple WebKit / 537.36(KHTML,如Gecko)Chrome / 44.0。 2403.157 Safari浏览器/ 537.36 )
//。数据( __ VIEWSTATE %2FwEPDwUHNjA4NDA1NQ9kFgQCAw9kFgoCAQ9kFgICAQ8PFgIeBFRleHQFClBUTSBLb3J0bnJkZAIC​​DxYCHgdWaXNpYmxlaGQCAw8WAh8BaGQCBA8WAh8BaGQCBQ8WAh8BaBYCAgEPEGRkFgBkAgUPDxYCHwAFCShkZXNrdG9wKWRkZGzBhwMIv3yxqKnC0C7%2BPlC0PlDG)
。数据( __ EVENTVALIDATION, %2FwEWBAKG7bXPBQLi0uqnCgKF69rWBAK14fOOCgrUt4CBVP4K0VKe0uOPxLSAu26y)
。数据( (存储卡号,)
.data(btnNext,N% C3%A4sta)
.cookies(loginForm.cookies())
.get();


解决方案

您绝对可以使用Jsoup。事实上,我已经使用Jsoup成功地从图书馆网站检索用户的信息。无论如何,我会为您提供完成此任务所遵循的步骤。



第1步:您输入的学生卡号码应该是输入一个表格。因此,只需右键单击您输入学号的区域,然后选择检查元素。您可以检查输入内容和表单。



第2步:仔细调查表单并记录<强>所有输入字段(包括隐藏字段)。您需要知道所有字段的名称。您还需要知道表单请求是在 GET POST 中进行的。



第3步:现在让我们来看看有趣的部分。使用下面的代码片段向服务器发送请求并检索表单。

  Connection.Response res = Jsoup.connect( url)
.userAgent(Mozilla / 5.0(Macintosh; Intel Mac OS X 10_10_5)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 44.0.2403.157 Safari / 537.36
.data(inputName1,inputValue1
inputName2,inputValue2)
.cookies(cookies)//如果不需要cookie,则不添加此行
.method(Method.GET)
.execute();

url 是请求地址,大小写为 http://kortladd3.chalmerskonferens.se/CardLoad_Order.aspx userAgent 告诉服务器您的浏览器详细信息, data 是您将所有输入字段的名称和值对放在第电子表格。 cookies 是您放置Cookie的位置,您需要检查您的请求是否需要cookie被服务器接受,可以在cookies部分的网络选项卡中进行检查(如果不需要cookie,请删除cookies行)。 方法指定您的请求方法。


$ b 检索的 res 对象将包含您需要的所有信息,html,cookies和所有内容。



这些仅仅是一般化的步骤。您可能需要根据您的需求添加更多代码,但基本上这是您处理请求和服务器的方式。


$ b 更新



我看到您传递了Cookie的硬编码字符串值,我相信这是您问题的原因。你如何检索你的cookies?您每次进行请求或登录时会话Cookie都会发生变化,您应该保存并动态传递您的Cookie。



您可以通过调用 res.cookies()


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();

解决方案

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()

这篇关于知道发送到服务器的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆