错误的请求错误

用户名

我已经为我的swing应用程序编写了一个代码来登录,我应该从Web服务获取标志作为json格式的响应,我面临的麻烦是,当我发布数据时,我的请求越来越糟糕400如果如果未设置标头,则设置某些标头或500个内部服务器错误。

连接等级

public class Connection extends Thread {

private String url1;
private JSONObject data;
String line;
//Constuctor to initialize the variables.

public Connection(String url1, JSONObject data) {
    this.url1 = url1;
    this.data = data;
    start();
}

public void run() {
    ConnectionReaderWriter();
}

//To fetch the data from the input stream
public String getResult() {
    return line;
}

public String ConnectionReaderWriter() {
    URL url;
    HttpURLConnection connection = null;
    ObjectOutputStream out;
    try {
        /*URL url = new URL(Url.server_url + url1);     //Creating the URL.       
         URLConnection conn = url.openConnection();    //Opening the connection.
         conn.setDoOutput(true);
         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
         wr.write(data);  //Posting the data to the ouput stream.
         wr.flush();
         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         line=rd.readLine();     //Reading the data from the input stream.       
         wr.close();
         rd.close();*/            
        url = new URL(Url.server_url + url1);     //Creating the URL.
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        // Headers
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        // API key has to be passed
        connection.setRequestProperty("api_key", "123456");
        connection.setUseCaches(false);
        //System.out.println(connection.getRequestProperty("api_key"));            
        connection.setDoInput(true);
        connection.setDoOutput(true);
        //Send request
        out = new ObjectOutputStream(connection.getOutputStream());
        out.write(data.toString().getBytes());
        out.flush();
        System.out.println(data.toString());
        int rescode = connection.getResponseCode();
        line = connection.getResponseMessage();
        System.out.println(line +" : "+ rescode);
        BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));            
        line = rd.readLine();     //Reading the data from the input stream.       
        rd.close();
        out.close();            
        System.out.println("fsgjv: ");

    } catch (MalformedURLException ex) {
        Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        String nonet = "No Network Connection";
        line = nonet;
    } catch (IOException ex) {
        Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        String nonet = "No Server Connection";
        line = nonet;
    }
    return line;  //Return te stream recived from the input stream.
}
}

产生的错误如下。

{"username":"[email protected]","password":"123"}
Bad Request : 400
Jan 20, 2014 6:00:04 PM SupportingClass.Connection ConnectionReaderWriter
SEVERE: null
java.io.IOException: Server returned HTTP response code: 400 for URL:     http://192.168.10.241/MobpazAdmin/web/app_dev.php/ws/terminalusers/terminaluserlogins.json
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672)
at java.security.AccessController.doPrivileged(Native Method)
at  sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670)
at  sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at SupportingClass.Connection.ConnectionReaderWriter(Connection.java:81)
at SupportingClass.Connection.run(Connection.java:40)
  Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL:    http://192.168.10.241/MobpazAdmin/web/app_dev.php/ws/terminalusers/terminaluserlogins.json
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at SupportingClass.Connection.ConnectionReaderWriter(Connection.java:78)
... 1 more 

如果我在代码中有任何错误,请告诉我,我已经检查了服务器端代码是否正常工作。要传递的数据是下面给出的json对象

{"username":"[email protected]","password":"123"}  
用户名

我现在使用Httpget实现相同的访问方式,因此我改变了这种访问方式。您必须下载相应的httpcomponents-client jar文件并将其添加到库中。使用的代码如下

data = URLEncoder.encode("searchvariable", "UTF-8") + "=" + URLEncoder.encode("name", "UTF-8");
HttpGet g = new HttpGet(Url.server_url + url1+"?"+data);

数据是要与URL一起传递的参数。发生此问题是由于错误的访问方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章