简体   繁体   English

如何将多部分HTTP请求从Perl发布到Java并获得响应?

[英]How can I POST a multipart HTTP request from Perl to Java and get a response?

I'm trying to post from one of my subroutines in Perl a request to a Java based controller. 我正在尝试从Perl中的一个子例程向基于Java的控制器发送请求。 But I'm not getting any kind of response back. 但我没有得到任何回应。 I know the Java code works file because I can get a response if I post to it from a HTML form. 我知道Java代码工作文件,因为如果我从HTML表单发布它,我可以得到响应。

This is my Perl code: 这是我的Perl代码:

  use HTTP::Request::Common;
  my $ua = LWP::UserAgent->new;

  my $response = $ua->request(POST 'http://testserver/testing.nc',
        Content_Type => 'form-data',
        Content => [
            method => 'submit',
            ftp_server => 'ftp.localhost',
            ftp_user => 'testuser',
            ftp_password => 'testpass',
            remote_path => '/home/files',
            port => 22,
            file_to_upload => ["$file"]
  ]);

Is there something wrong with this code? 这段代码有问题吗?

Posted data must be of type multipart/form-data . 发布的数据必须是multipart/form-data类型

Edit: OK, so it turns out, specifying form-data is enough as mentioned in the HTTP::Request::Common docs: 编辑:好的,事实证明,指定form-data就足够了,如HTTP :: Request :: Common文档中所述:

The POST method also supports the multipart/form-data content used for Form-based File Upload as specified in RFC 1867. You trigger this content format by specifying a content type of form-data as one of the request headers. POST方法还支持用于RFC 1867中指定的基于表单的文件上载的multipart/form-data内容。通过将form-data的内容类型指定为请求标头之一,可以触发此内容格式。

However, to use HTTP::Request::Common::POST the way you are using, you will need to import POST : 但是,要以您使用的方式使用HTTP::Request::Common::POST ,您需要导入POST

use HTTP::Request::Common qw(POST);

or use $ua->post : 或使用$ua->post

The post(...) method of LWP::UserAgent exists as a shortcut for $ua->request(POST ...) . LWP :: UserAgentpost(...)方法作为$ua->request(POST ...)的快捷方式存在。

You can make your life easier by using WWW::Mechanize . 使用WWW :: Mechanize可以让您的生活更轻松。 See also this upload example . 另请参阅此上传示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM