我正在尝试从Java发送请求到PHP页面。
<?php
include "connect.php";
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$result = signin();
if($result == "1"){
echo "1";
}else{
echo "0";
}
function signin(){
global $uname;
global $pass;
global $db;
$connect = $db->prepare("SELECT * FROM user where uname=? and pass=?");
$connect->execute(array($uname,$pass));
$result = $connect->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row){
return 1;
}
return 0;
}
?>
我的Java方法:
public int checkPass(){
try{
// open a connection to the site
URL url = new URL("http://192.168.1.35/userControls/singin.php");
URLConnection con = (HttpURLConnection)url.openConnection();
((HttpURLConnection) con).setRequestMethod("POST");
// activate the output
con.setDoOutput(true);
//con.setDoInput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
ps.print(URLEncoder.encode("?uname","UTF-8") + "=" + URLEncoder.encode(uname, "UTF-8"));
ps.print(URLEncoder.encode("&pass=","UTF-8")+ "=" + URLEncoder.encode(pass, "utf-8"));
ps.close();
BufferedReader cin = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = cin.readLine()) != null) {
System.out.println(":::::::Worked::::::::: " + line);
}
}/* catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/ catch (Exception e) {
System.out.println("Eror:" + e.getMessage());
}
return 1;
}
但是当我运行它时,错误:( INVALID HTTP RESPONSE)
java.io.IOException:无效的Http响应 at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1555) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) 在CapitalizeServer $ Capitalizer.checkPass(CapitalizeServer.java:171) 在CapitalizeServer $ Capitalizer.run(CapitalizeServer.java:64)
CapitalizeServer.java的第171行是:
BufferedReader cin = new BufferedReader(new InputStreamReader(con.getInputStream()));
我正在使用WAMP Server-2.4.9
我该如何解决这个问题?