如何从Perl向CGI程序发送XML?

时间:2022-01-31 20:46:44

I want to send some XML from a Perl program to a CGI script that makes use of XML::Simple to take that XML as input and send XML as output.

我想将一些XML从Perl程序发送到CGI脚本,该脚本利用XML :: Simple将该XML作为输入并将XML作为输出发送。

Is there a way to send XML to a CGI script from Perl? Any help in this regards would be really appreciated.

有没有办法从Perl向CGI脚本发送XML?在这方面的任何帮助将非常感谢。

Thank You

3 个解决方案

#1


One of the possible solutions would be use the HTTP::Request::Common module, which exposes some useful functions like GET, POST and HEADER.

其中一个可能的解决方案是使用HTTP :: Request :: Common模块,该模块公开了一些有用的函数,如GET,POST和HEADER。

Assuming you want to use POST to send the data to the remote application, you could do:

假设您要使用POST将数据发送到远程应用程序,您可以执行以下操作:

use HTTP::Request::Common;
use LWP::UserAgent;

my $url = 'http://localhost/cgi-bin/mycgi.pl';
my $xml = "<root></root>";
my $request = POST $url, Content_Type => 'text/xml; charset=utf-8', Content => $xml;
my $ua = LWP::UserAgent->new();
my $response = $ua->request($request);
if ( $response->is_success() ) {
    print $response->content();
}
else {
    warn $response->status_line, $/;
}

Hope this helps!

希望这可以帮助!

#2


Assuming you have the XML in your program already; it is just an HTTP request, so LWP is your friend. The specifics depend on how the CGI program expects the XML to be passed (e.g. as POSTed url-encoded-form-data, multi-part MIME, etc)

假设你的程序中已经有了XML;它只是一个HTTP请求,所以LWP是你的朋友。具体取决于CGI程序如何期望传递XML(例如,POST为url-encoded-form-data,多部分MIME等)

#3


There's nothing special about XML: it's just text. Send it like you would send any other text. Is there something else that isn't working for you? What have you tried already?

XML没有什么特别之处:它只是文本。像发送任何其他文本一样发送它。还有其他不适合你的东西吗?你有什么尝试?

If you're having trouble sending anything to the CGI program, take a look at a framework such as WWW::Mechanize which does most of the work of the request and response loop for you.

如果您在向CGI程序发送任何内容时遇到问题,请查看WWW :: Mechanize等框架,它可以为您完成请求和响应循环的大部分工作。

#1


One of the possible solutions would be use the HTTP::Request::Common module, which exposes some useful functions like GET, POST and HEADER.

其中一个可能的解决方案是使用HTTP :: Request :: Common模块,该模块公开了一些有用的函数,如GET,POST和HEADER。

Assuming you want to use POST to send the data to the remote application, you could do:

假设您要使用POST将数据发送到远程应用程序,您可以执行以下操作:

use HTTP::Request::Common;
use LWP::UserAgent;

my $url = 'http://localhost/cgi-bin/mycgi.pl';
my $xml = "<root></root>";
my $request = POST $url, Content_Type => 'text/xml; charset=utf-8', Content => $xml;
my $ua = LWP::UserAgent->new();
my $response = $ua->request($request);
if ( $response->is_success() ) {
    print $response->content();
}
else {
    warn $response->status_line, $/;
}

Hope this helps!

希望这可以帮助!

#2


Assuming you have the XML in your program already; it is just an HTTP request, so LWP is your friend. The specifics depend on how the CGI program expects the XML to be passed (e.g. as POSTed url-encoded-form-data, multi-part MIME, etc)

假设你的程序中已经有了XML;它只是一个HTTP请求,所以LWP是你的朋友。具体取决于CGI程序如何期望传递XML(例如,POST为url-encoded-form-data,多部分MIME等)

#3


There's nothing special about XML: it's just text. Send it like you would send any other text. Is there something else that isn't working for you? What have you tried already?

XML没有什么特别之处:它只是文本。像发送任何其他文本一样发送它。还有其他不适合你的东西吗?你有什么尝试?

If you're having trouble sending anything to the CGI program, take a look at a framework such as WWW::Mechanize which does most of the work of the request and response loop for you.

如果您在向CGI程序发送任何内容时遇到问题,请查看WWW :: Mechanize等框架,它可以为您完成请求和响应循环的大部分工作。