Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws Exception {
- sendPost();
- }
- private static void sendPost() throws Exception {
- String url = "https://exampleSite.com";
- URL obj = new URL(url);
- HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
- String account = "acc";
- String password = "pass";
- //add reuqest header
- con.setRequestMethod("POST");
- con.setRequestProperty("Content-type", "text/xml");
- String urlParameters = "account=" + account + "&password=" + password;
- // Send post request
- con.setDoOutput(true);
- DataOutputStream wr = new DataOutputStream(con.getOutputStream());
- wr.writeBytes(urlParameters);
- wr.flush();
- wr.close();
- int responseCode = con.getResponseCode();
- System.out.println("nSending 'POST' request to URL : " + url);
- System.out.println("Post parameters : " + urlParameters);
- System.out.println("Response Code : " + responseCode);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- //print result
- System.out.println(response.toString());
- }
Add Comment
Please, Sign In to add comment