我正在开发一个Java程序,需要用户登录,但是当结束HTTP请求并尝试读取输出时,它会返回HTTP响应代码500.
Login.java中的代码如下:
package net.discfiresoftworks.chat;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Login extends JFrame
{
private static final long serialVersionUID = 1L;
public JTextField user = new JTextField();
public JPasswordField pass = new JPasswordField(20);
public JButton submit = new JButton("Log In");
public JButton create = new JButton("Sign-Up");
public JPanel main = new JPanel();
public JPanel buttons = new JPanel();
public Login()
{
setTitle("DiscFire Account Login");
setSize(350, 180);
setDefaultCloseOperation(3);
setLayout(new FlowLayout(1));
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setBackground(new Color(50, 50, 50));
user.setColumns(20);
pass.setColumns(20);
pass.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(10, 0, 10, 0), pass.getBorder()));
submit.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
create.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
submit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
String password = new String(pass.getPassword());
sendRequest(user.getText(), password);
}
});
create.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
}
});
buttons.setLayout(new FlowLayout());
buttons.add(submit);
buttons.add(create);
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
main.setBorder(BorderFactory.createLineBorder(Color.WHITE, 10));
main.add(user);
main.add(pass);
main.add(buttons);
add(main);
}
public void open()
{
setVisible(true);
}
public void sendRequest(String user, String pass)
{
String output = "";
try
{
String urlParameters = "user=" + URLEncoder.encode(user,"UTF-8") + "&pass=" + URLEncoder.encode(pass,"UTF-8");
URL url = new URL("http://www.discfiresoftworks.net/plogin.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
System.out.println(urlParameters);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
output = in.readLine();
in.close();
connection.disconnect();
}catch(Exception e){
e.printStackTrace();
}
String sid = (output.split(":"))[0];
String admin = (output.split(":"))[1];
System.out.println(sid = " " + admin);
}
}
plogin.php中的代码如下:
<?php
include 'includes/conn.php';
include 'includes/hash.php'
session_start();
if(isset($_POST['user']) && isset($_POST['pass'])){
$user = $_POST['user'];
$password = $_POST['pass'];
if(isset($_POST['redir'])){
$redir = $_POST['redir'];
}else{
$redir = '';
}
$query = $conn->prepare("SELECT Password, Verified, Admin FROM Users WHERE Name=?");
$query->bind_param('s', $user);
if($query->execute()){
$query->bind_result($hash, $ver, $admin);
while($query->fetch()){
if($ver == "T"){
if($query->num_rows > 0){
if(validate_password($password, $hash)){
$_SESSION['user'] = $user;
$msg = session_id() . ':' . $user;
}else{
$msg = 'Incorrect username or password';
}
}else{
$msg = 'Incorrect username or password';
}
}else{
$msg = 'Your account has not been verified, please check your email and do so now if you wish to continue.';
}
}
}else{
$msg = $query->errno . " " . $query->error;
}
}else{
$msg = 'Missing username or password';
}
echo $msg;
?>
提前致谢,我希望你能帮助我:)。
编辑:我知道这不是conn.php或hash.php中的错误,因为我之前使用过它们并且它们正常工作。
编辑:Stacktrace:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.discfiresoftworks.net/plogin.php
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at net.discfiresoftworks.chat.Login.sendRequest(Login.java:116)
at net.discfiresoftworks.chat.Login$1.actionPerformed(Login.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at net.discfiresoftworks.chat.Login.sendRequest(Login.java:128)
at net.discfiresoftworks.chat.Login$1.actionPerformed(Login.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:0)
我遇到了同样的问题,现在解决了。 只需检查编码是否正确。确保您已将所有内容编码为同一个字符集。 在我的情况下,我使用ISO8859_1编码。服务器响应okfor firefox浏览器但错误500到我的java程序。我改为ISO8859_1,现在它正在运作:
public void sendPost(String Url, String params) throws Exception {
String url=Url;
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestProperty("Acceptcharset", "en-us");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("charset", "EN-US");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
String urlParameters=params;
// Send post request
con.setDoOutput(true);
con.setDoInput(true);
con.connect();
//con.
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());
this.response=response.toString();
con.disconnect();
}
在主程序中我称之为:
certcon.sendPost("https://www.desphilboy.com/authbox/generateCRT","csr_string="+URLEncoder.encode(requestparams,"ISO8859_1"));