package com.company; import javax.net.ssl.HttpsURLConnection; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Main { private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { Main http = new Main(); System.out.println("Testing 1 - Send Http GET request"); http.sendGet(); System.out.println("\nTesting 2 - Send Http POST request"); http.sendPost(); } // HTTP GET request private void sendGet() throws Exception { String url = "http://sysadmins.ru/login.php"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); //add request header con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); 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()); } // HTTP POST request private void sendPost() throws Exception { String url = "http://sysadmins.ru/login.php"; URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = "username=Megatron666&password=123456&redirect=&login=%C2%F5%EE%E4"; // 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()); } } Closed due to the fact that off-topic participants D-side , Dmitriy Simushev , cheops , aleksandr barakin , YuriySPb ♦ 14 May '16 at 22:12 .
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
- “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - D-side, Dmitriy Simushev, cheops, aleksandr barakin, YuriySPb
|
1 answer
- I do not see anything like working with cookies.
- The server can redirect, from which cookies are lost. In this case, you should disable the automatic execution of the redirect and get cookies.
%C2%F5%EE%E4- win1251 in parameters - is this exactly normal?"Mozilla/5.0"- it does not pull on the normal useragent.
PS: Additional useful information on the implementation of the input (code on VB.NET).
|