获取HTTP响应代码500以在localhost上发布

时间:2014-06-25 20:39:30

标签: java http-post http-get

我不知道如何在localhost网络服务器上发布内容。我甚至不知道如何让网络服务器处理POST请求。我尝试了以下代码并设法在API上发布,但无法在我的localhost上执行相同操作。

 public class JSONTest {
   public static String executePost(String targetURL, String urlParameters){
    URL url;
    HttpURLConnection connection = null;  
    try {
      //Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");  
      connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  
      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      //Send request
      DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();

      //Get Response    
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer(); 
      while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
      }
      rd.close();
      return response.toString();
    }catch (Exception e) {
      e.printStackTrace();
      return null;

    }
    finally {
      if(connection != null)
        connection.disconnect(); 
    }       
}
 public static String test(){
    try{
    JSONObject obj = new JSONObject();
    //JSONArray array = new JSONArray();
    obj.put("Products","Successful test");
    return "Products="+obj.toString();
    } catch (Exception e){
        e.printStackTrace();
    }
    return null;
}
public static void main(String[]args){
   executePost("http://<localhost>:9000/SelendroidManager",test());
}
}

我收到以下错误:

java.io.IOException: Server returned HTTP response code: 500 for URL: http://<localhost>:9000/SelendroidManager
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at Administrator.JSONTest.executePost(JSONTest.java:65)
at Administrator.JSONTest.main(JSONTest.java:144)

服务器的日志输出为:

 Reapplying settings...
 Set current project to lol (in build file:/C:/Users/eedcoro/activator-1.2.2-minimal/lol/)
 Reapplying settings...
 Set current project to lol (in build file:/C:/Users/eedcoro/activator-1.2.2-minimal/lol/)
 Running task... Cancel: sbt.TaskCancellationStrategy$Null$@7b6bbeaf, check cycles: false
 Initial source changes: removed:Set() added: Set() modified: Set() Removed products: Set() External API changes: API Changes: Set() Modified binary dependencies: Set() Initial directly invalidated sources: Set() Sources indirectly invalidated by: product: Set() binary dep: Set() external source: Set()
 All initially invalidated sources: Set()
 Discovered main classes:
 Got auto-discovered main classes, looking for a default mainClass setting if any
 launching mainClass task
 Set current project to lol (in build file:/C:/Users/eedcoro/activator-1.2.2-minimal/lol/)
 > listen
 Reapplying settings...
 Set current project to lol (in build file:/C:/Users/eedcoro/activator-1.2.2-minimal/lol/)
 Reapplying settings...
 Set current project to lol (in build file:/C:/Users/eedcoro/activator-1.2.2-minimal/lol/)
 Running task... Cancel: sbt.TaskCancellationStrategy$Null$@7b6bbeaf, check cycles: false
 Default main class is 'play.core.server.NettyServer'

0 个答案:

没有答案