X
X
xbs72018-05-25 10:32:15
Java
xbs7, 2018-05-25 10:32:15

How to send request from java to php file?

Hello. Actually a question. There is a program that sends a GET request to the site.ru server and accesses the file.php file and pulls out, as I understand it, a parameter or variable (I don’t understand) code. Actually a question. How to do all this in Java? You need to get information from this file. The server is not protected, requests are also sent without keys. Thanks in advance. Part when programs

type: 'GET',
cache: false,
data: { get:"code";}.

Well, here is my program.
package exapmle11;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Exapmle11 {
public static void main(String[] args) {
  String query = "http://site.ru/file.php";

  HttpURLConnection connection = null;
  try{
  connection = (HttpURLConnection) new URL(query).openConnection();

  connection.setRequestMethod("GET");
  connection.setUseCaches(false);
  connection.setConnectTimeout(0);
  connection.setReadTimeout(0);

  connection.connect();
  StringBuilder sb=new StringBuilder();

  if(HttpURLConnection.HTTP_OK == connection.getResponseCode())
  {BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  String line;
  while((line = in.readLine()) != null){
      sb.append(line);
      sb.append("\n");
  }
      System.out.println(sb.toString()); 
  } else {System.out.println("fail:"+connection.getResponseCode()+","+connection.getResponseMessage());
          }
}
catch(Throwable cause){
    cause.printStackTrace();
} finally{
      if(connection!=null){
          connection.disconnect();}
}
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question