对Rails应用程序的HTTP POST请求不成功

时间:2014-11-26 17:24:04

标签: java ruby-on-rails post request

我未能成功尝试从运行在Windows上的Java应用程序向同一台计算机上的虚拟机上运行的Rails应用程序发送HTTP POST请求。下面是来自Java端的HTTP POST请求和在Rails端接收它的代码。我收到的错误(在Java的Eclipse IDE上)是" java.net.ConnectException:连接定时out:connect"

有人可以帮我理解我出错的地方,特别是我可以在Rails方面做些什么来接收帖子请求。

这是Java HTTP POST请求:

public void sendHTTPPOSTRequestReport(int c, int s){

try {
    //*****************************************************************************************
    //JSON Object Created Here
    JSONObject reportObject = new JSONObject();
    reportObject.put("student_id", myClass[c].getStudentElement(s).getStudentNumber());
    reportObject.put("student_name", myClass[c].getStudentElement(s).getName());
    reportObject.put("school_name", getSchoolName());
    reportObject.put("instructor_name", getInstructorName());
    reportObject.put("course_name", myClass[c].getClassName());
    reportObject.put("absent_days", String.valueOf(myClass[c].getStudentElement(s).getAbsences()));
    reportObject.put("tardy_days", String.valueOf(myClass[c].getStudentElement(s).getTardies()));
    reportObject.put("total_grade", String.valueOf(myClass[c].getStudentElement(s).getTotalGrade()));

    JSONArray assignmentArray = new JSONArray();

    for(int a=0; a<20; a++){

        JSONObject assignmentObject = new JSONObject();

        assignmentObject.put("assignment_name", myClass[c].getAssignmentElement(a).getName());
        assignmentObject.put("assignment_date", String.valueOf(myClass[c].getAssignmentElement(a).getDate()));
        assignmentObject.put("assignment_category",myClass[c].getAssignmentElement(a).getCategory());
        assignmentObject.put("assignment_score", String.valueOf(myClass[c].getAssignmentElement(a).getGrade()));

        assignmentArray.add(assignmentObject);
    }
    reportObject.put("assignments", assignmentArray);
    System.out.println(reportObject);
    //*******************************************************************************************

    //Base64 Conversions
    //name:password converts to bmFtZTpwYXNzd29yZA==
    //“name:password” converts to Im5hbWU6cGFzc3dvcmQi
    String basic = "Im5hbWU6cGFzc3dvcmQi";//HTTP Basic Authentication


    String hostname = "192.168.80.2";//This is the static IP of the virtual machine.
    InetAddress addr = InetAddress.getByName(hostname);
    int port = 80;
    Socket socket = new Socket(addr, port);

    String path = "http://0.0.0.0:3000";

    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
    wr.write("POST"+path+" HTTP/1.0rn");
    wr.write("Authorization: Basic "+basic+"\r\n");
    wr.write("Content-Length: "+((CharSequence) reportObject).length()+"rn");
    //wr.write("Content-Type: application/x-www-form-urlencodedrn");
    wr.write("Content-Type: application/json");
    wr.write("rn");
    // Send parameters
    //wr.write(obj);//This was the format from an example but had to be modified as follows:
    reportObject.writeJSONString(wr);
    wr.flush();
    // Get response
    BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
    socket.close();//Should not close this socket but leave it open until program closes
}catch (Exception e) {e.printStackTrace();}

}

这是Rails方面的代码:

class PostsController < ApplicationController

  http_basic_authenticate_with name: "name", password: "password"

  def index
    render plain: "Text Goes Here!"
  end

  def edit
    render plain: "Text Goes Here."
  end

end

0 个答案:

没有答案