Answer the question
In order to leave comments, you need to log in
How to receive data from the server?
Hello, I wanted to send a request through a java "app" (just a piece of code at the moment). After sending a request to the httpbin.org/post site, I get a json response, and when I send a request to a file that processes the data entered by the user on the site, there is nothing. I'm using Open Server to test all of this.
The site itself:
<form action="test.php" method="POST">
<input type="text" name="user_name">
<input type="text" name="user_password">
<select name="city_id">
<option value="1">Саратов</option>
<option value="2">Омск</option>
<option value="3">СПБ</option>
</select>
<input type="submit" value="Отправить данные">
</form>
<?php
$fam = $_POST['fam'];
$lschet = $_POST['lschet'];
$c_id = $_POST['c_id'];
$fam = htmlspecialchars($fam);
$lschet = htmlspecialchars($lschet);
$c_id = htmlspecialchars($c_id);
echo $fam;
echo $lschet;
echo $c_id;
echo "Hi";
?>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String urlAress = "http://form/test.php";
URL url;
HttpURLConnection httpURLConnection;
OutputStream os = null;
InputStreamReader isR = null;
BufferedReader bfR = null;
StringBuilder stringBuilder = new StringBuilder();
try{
Map<String, String> postArgs = new HashMap<>();
postArgs.put("fam", "SGHfdsgdh");
postArgs.put("lschet", "123456789101");
postArgs.put("c_id", "1");
byte[] out = postArgs.toString().getBytes();
url = new URL(urlAress);
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.addRequestProperty("User-Agent", "Mozilla/5.0");
httpURLConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setConnectTimeout(200);
httpURLConnection.setReadTimeout(200);
httpURLConnection.connect();
try{
os = httpURLConnection.getOutputStream();
os.write(out);
} catch(Exception e){
System.err.print(e.getMessage());
}
if(HttpURLConnection.HTTP_OK == httpURLConnection.getResponseCode()){
isR = new InputStreamReader(httpURLConnection.getInputStream());
bfR = new BufferedReader(isR);
String line;
while((line=bfR.readLine()) != null){
stringBuilder.append(line);
}
}
//System.out.println(postArgs);
int status = httpURLConnection.getResponseCode();
System.out.println(status);
System.out.println(stringBuilder);
} catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e ){
System.err.print(e.getMessage());
} finally{
try{
isR.close();
} catch(IOException e){
e.printStackTrace();
}
try{
bfR.close();
} catch(IOException e){
e.printStackTrace();
}
try{
os.close();
} catch(IOException e){
e.printStackTrace();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Let's start with the fact that your test.php does not return json, but simply the data in text format sent from the form (even without spaces), the answer will be something like this:
SGHfdsgdh1234567891011 - this is what you are trying to send from Java,
rewrite something like this:
$data = [];
$data['fam'] = htmlspecialchars($_POST['fam']);
$data['lschet'] = htmlspecialchars($_POST['lschet']);
$data['c_id'] = htmlspecialchars($_POST['c_id']);
echo json_encode($data);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question