JSOUP Submitting a POST to a form

2019-08-08 15:21发布

问题:

I'm currently working on my programming skills by doing a application for a website. For now I can sign in to the webpage trough the application. My focus now is to let the user change his/her's profile trough the application and save it to the website. The problem is that nothing is happening on the website and I get no error code or anything.

When I click the save button to save recently changes on the website I get the following header message (By using google chrome f12 -> Network):

firstname: John
lastname:Random
streetAddress:Woodstreet 12
careOfAddress:
zipCode:417 22
city:Woods
country:US
phone:111122000
languageForeignKey:4FCB3B38F96800010003004E
invoiceDeliveryMethod:email
preferredMessageMethod:sms
save_button:
utf8:✓
authenticity_token:+RadnomKey40134128Notes=

My method

public void setUpdatedContactInformation(String cookie, String firstName,
        String lastName, String streeAdress, String coAdress,
        String zipCode, String city, String country,
        String alternativPhone, String language, String deliveryMethod,
        String messageMethod, String token) {
    try {
        Response Jresponse = Jsoup
                .connect(ContactInfoURL).cookie("Website-session", cookie)
                .data("firstname", firstName, "lastname", lastName,
                        "streetAddress", streeAdress, "careOfAddress",
                        coAdress, "zipCode", zipCode, "city", city,
                        "country", country, "phone", alternativPhone,
                        "languageForeignKey", language,
                        "invoiceDeliveryMethod", deliveryMethod,
                        "preferredMessageMethod", messageMethod,
                        "authenticity_token", token)
                .method(Method.POST)
                .execute();

I don't know if token is needed, but I used it anyway. I can fetch the token from before so it's the same as the logged session. Every input that the functions takes is the same as the form, I just changed the firstname to Johnnn to see if it works, but John is not changed on the website.

回答1:

Fixed it!

There were several buttons on the page (a search bar on the top and some navigation buttons). By editing the code and add the button that I was focusing on resolved the issue. New Code:

esponse Jresponse = .connect(ContactInfoURL).cookie("Website-session", cookie)
            .data("firstname", firstName, "lastname", lastName,
                    "streetAddress", streeAdress, "careOfAddress",
                    coAdress, "zipCode", zipCode, "city", city,
                    "country", country, "phone", alternativPhone,
                    "languageForeignKey", language,
                    "invoiceDeliveryMethod", deliveryMethod,
                    "preferredMessageMethod", messageMethod, "save_button", button,
                    "authenticity_token", token)
                .method(Method.POST)
                .execute();

So by basically adding the name of the button which can be captured by F12->Chrome fixed it. :)