如何将来自 Perl 的多部分 HTTP 请求 POST 到 Java 并获得响应?

发布于 2024-08-23 03:02:27 字数 648 浏览 5 评论 0原文

我正在尝试从 Perl 中的一个子例程向基于 Java 的控制器发出请求。 但我没有得到任何回复。我知道 Java 代码工作文件,因为如果我从 HTML 表单发布到它,我可以获得响应。

这是我的 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"]
  ]);

这段代码有问题吗?

I'm trying to post from one of my subroutines in Perl a request to a Java based controller.
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.

This is my Perl code:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛泛之交 2024-08-30 03:02:27

发布的数据必须为multipart/form-类型数据

编辑:好的,事实证明,指定form-data就足够了,正如HTTP::Request::Common 文档:

POST 方法还支持 RFC 1867 中指定的用于基于表单的文件上传的 multipart/form-data 内容。您可以通过指定 form 的内容类型来触发此内容格式-data 作为请求标头之一。

但是,要按照您使用的方式使用 HTTP::Request::Common::POST,您需要导入 POST:

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

或使用 $ua-> ;帖子:

LWP:: 的 post(...) 方法UserAgent 作为 $ua->request(POST ...) 的快捷方式存在。

使用 WWW::Mechanize 可以使您的生活更轻松。另请参阅此上传示例

Posted data must be of type multipart/form-data.

Edit: OK, so it turns out, specifying form-data is enough as mentioned in the HTTP::Request::Common docs:

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.

However, to use HTTP::Request::Common::POST the way you are using, you will need to import POST:

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

or use $ua->post:

The post(...) method of LWP::UserAgent exists as a shortcut for $ua->request(POST ...).

You can make your life easier by using WWW::Mechanize. See also this upload example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文