【问题标题】:Java: HTTP Post to create new “Ride” in a Ruby on Rails applicationJava:HTTP Post 在 Ruby on Rails 应用程序中创建新的“Ride”
【发布时间】:2009-11-05 05:21:56
【问题描述】:

我的问题与Java: HTTP Post to create new "Product" in a Ruby on Rails application非常相似

我有一个表格,我想将其“发布”到一个看起来像这样的表格

Offer:<br /> 
<input id="ride_togive" name="ride[togive]" size="30" type="text" /> 
</p> 
<p> 
Request for Offer:<br /> 
<input id="ride_totake" name="ride[totake]" size="30" type="text" /> 
</p> 

我的 Java 代码如下所示

DefaultHttpClient client = new DefaultHttpClient();

         HttpPost post = new HttpPost("http://172.16.60.129:3000/rides");

     // Configure the form parameters
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("ride_totake", "Ride to Vail"));
        nvps.add(new BasicNameValuePair("ride_togive", "$20"));

        post.addHeader("Content-Type","application/json");

        try {
            post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        HttpResponse response = null;
        try {
            response = client.execute(post);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        System.out.println("form get: " + response.getStatusLine());
        if (entity != null) {
            try {
                entity.consumeContent();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

这是 RoR 控制台的输出

Processing RidesController#create (for 172.16.60.1 at 2009-11-04 22:22:52) [POST]
Parameters: {"_json"=>"totake=Ride+to+Vail&togive=%2420"}
Ride Columns (0.6ms)   SHOW FIELDS FROM `rides`
SQL (0.1ms)   BEGIN
Ride Create (0.3ms)   INSERT INTO `rides` (`isoffer`, `togive`, `howlong`, `updated_at`,       `totake`, `created_at`) 
VALUES(NULL, NULL, 0, '2009-11-05 05:22:52', NULL, '2009-11-05    05:22:52')
SQL (0.0ms)   COMMIT
Redirected to http://172.16.60.129:3000/rides
Completed in 9ms (DB: 1) | 302 Found [http://172.16.60.129/rides]

我想我真正的问题是我应该如何处理 Java 中的 RoR 变量?

【问题讨论】:

  • 我不确定您的意思是 RoR 变量,但是当所有事情都通过 HTTP POST 或 GET 发生时,那么所有这些值都将在 HTTP 响应中可用,对吧?
  • 我所说的 RoR 变量是指由 RoR 生成的 html 表单,它位于我的问题的顶部。如何将值插入到 java 中的这些字段中?

标签: java ruby-on-rails httpclient


【解决方案1】:

在您的 html 输入标签中使用名称而不是 id。在您的示例中,它们是“ride[togive]”和“ride[totake]”。

适用于我的 RoR 项目的示例 Java 代码。使用 HttpClient 3.1

    PostMethod post = new PostMethod("http://localhost:3000/projects");
NameValuePair[] data = {
  new NameValuePair("project[name]", "from java"),
  new NameValuePair("project[life_cycle_id]", "5")
};
post.setRequestBody(data);
// execute method and handle any error responses.

new HttpClient().executeMethod(post);

从 RoR 控制台:

Processing ProjectsController#create (for 127.0.0.1 at 2009-11-04 22:07:39) [POS
T]
  Parameters: {"project"=>{"name"=>"from java", "life_cycle_id"=>"5"}}

【讨论】:

  • 我尝试了这个,当我这样做时,我得到了与我在帖子中发布的相同的 RoR 终端响应,togive 和 totake 值为空。有点奇怪。还有其他想法吗?
  • 修改后的答案。将 RoR 控制台与您的输出进行比较: 参数:{"_json"=>"totake=Ride+to+Vail&togive=%2420"} 注意:在我的示例中,我不必将内容类型设置为 application/json。跨度>
  • 不幸的是,这个答案看起来像是使用旧的 3.1 的 HttpClient,如果想用 4.1 运行它是不可能的。我尝试降级到 3.1,但它导致与 AuthenticityToken 冲突,我收到了 ActionController::InvalidAuthenticityToken。我相信当内容类型设置为 json 时,它会处理这个错误。
  • 只是为了确认一下,一旦我在 RoR 端禁用了 AuthenticityToken,我确实让这段代码工作了。如果有人知道如何使用 AuthenticityToken 让我知道。 -帕特
猜你喜欢
  • 2010-10-16
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多