Programmatically click a webpage button

后端 未结 3 952
心在旅途
心在旅途 2020-12-18 06:18

How to programmatically click a webpage button in Java?

Given this button:

 
B
3条回答
  •  [愿得一人]
    2020-12-18 06:41

    I'm not sure if this is what you are looking for, but if what you are looking to do is simply make a HTTP request in Java, this should do the trick:

    HttpURLConnection urlConnection = null;
    URL urlToRequest = new URL("http://yoursite.com/yourpage?key=val&key1=val1");
    urlConnection = (HttpURLConnection) urlToRequest.openConnection();
    urlConnection.setConnectTimeout(CONN_TIMEOUT);
    urlConnection.setReadTimeout(READ_TIMEOUT);
    
    // handle issues
    int statusCode = urlConnection.getResponseCode();
    if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        // handle unauthorized (if service requires user login)
    } else if (statusCode != HttpURLConnection.HTTP_OK) {
        // handle any other errors, like 404, 500,..
    }
    
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    
    // Process response
    

    Replace the timeout constants with your own values, and the URL to match that of your website. You can send any data you want to send with the click of the button as query parameters in the URL in the form of key/value pairs.

提交回复
热议问题