对于如何在Perl中发出XML请求-需要帮助吗? [英] For How to make an XML Request in Perl -help required?

查看:78
本文介绍了对于如何在Perl中发出XML请求-需要帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是Perl和XML的新手.任何人都可以指导我执行以下操作: 我必须编写一个Perl代码,该代码应该可以完成:

  1. 建立连接.

  2. 获取连接的响应,即connectionId并将其存储在变量中.

  3. 关闭连接

输入:

应发布XML请求.请求类型为http.

我和我在一起的XML语法.

任何人都可以指导我应采取哪些步骤来满足上述要求.

预先感谢

添加代码:

use LWP::UserAgent;
use HTTP::Request::Common;
# The xml_request
my $xml_req = "<?xml version=1.0 encoding UTF-8?>
            <!ELEMENT drl (openconnection)>
            <!ATTLIST drl
             mode normal
             connectionid null
            >
            <!ELEMENT openconnection EMPTY>
            <!ATTLIST openconnection
            username admin
            password admin
            ></drl>";
my $ua = LWP::UserAgent->new();
my $response = $ua->post("http://XX.X.X.X:XXXX/lab/v1_2/connection/openConnectionRequest.dtd", Content => $xml_req);

输出是一个哈希,但是如果它应该返回和id是一个No ..我要去哪里呢?

解决方案

根据您的请求的性质(大小,内容,结构取决于输入的多少),有几种不同的方法来实现. /p>

最简单的方法是将您的请求存储在程序的字符串中,放入变量,然后发送.

#!/usr/bin/perl
use strict; use warnings;
use LWP::UserAgent;

my ($param1, $param2) = (1, 2);

my $xml = <<XMLREQUEST
<request>
  <param1>$param1</param1>
  <param2>$param2</param2>
</request>
XMLREQUEST
;

my $ua = LWP::UserAgent->new;
my $response = $ua->post('http://www.yourdomain.com/', Content => $xml);
if ($response->is_success) {
  print $response->decoded_content;  # or whatever
}
else {
  die $response->status_line;
}

在这种情况下,我们使用的是 LWP::UserAgent 为我们进行发送/接收.

下一步是使用模板引擎.可以使用 Text::Template 完成.我们的示例代码如下所示:

#!/usr/bin/perl
use strict; use warnings;
use LWP::UserAgent;
use Text::Template;

my $vars = {
  'username' => 'jon',
  'password' => 'verysecure',
  'param1' => {
    'content' => 'a lot of content for param1',
    'foo'     => 'fofofofooo',
  },
  'param2' => {
    'content' => 'even more of content for param2',
    'bar'     => 'bar bar bar',
  },
  'param3' => {
    'content' => 'some content for param3',
  },
};

my $template = Text::Template->new(TYPE => 'FILEHANDLE', SOURCE => \*DATA );
my $xml = $template->fill_in(HASH => $vars);  

my $ua = LWP::UserAgent->new;
my $response = $ua->post('http://www.yourdomain.com/', Content => $xml);
if ($response->is_success) {
  print $response->decoded_content;  # or whatever
}
else {
  die $response->status_line;
}

__DATA__
<request>
  <auth>
    <username>{$username}</username>
    <password>{$password}</password>
  </auth>
  <param1 foo="{$param1{'foo'}}">{$param1{'content'}}</param1>
  <param2 bar="{$param2{'bar'}}">{$param2{'content'}}</param2>
  <param3>{$param3{'content'}}</param3>
</request>

它使用$vars hashref并将其内容放在模板中的相应位置.在模板显示{$username}的位置填充$var->{'username'}.在这种情况下,可从程序下方指定的DATA部分读取模板.一个好的方法是为您需要执行的每种请求类型准备一个模板文件.如果请求中包含可选元素,则Text::Template可以通过条件语句(在模板中放入Perl代码)来解决此问题.

如果您希望使用更动态的方法,请考虑 完成简单的任务.请记住,XML::Simple并不是最好的XML模块.还有其他一些功能,例如XML::Twig更为强大.

#!/usr/bin/perl
use strict; use warnings;
use LWP::UserAgent;
use XML::Simple;

my $xmlHash = {
  'request' => {
    'auth' => [ 
      {
        'username' => 'jon', 
      },
      {
        'password' => 'verysecure',
      },
    ],
    'param1' => {
      'content' => 'a lot of content for param1',
      'foo'     => 'fofofofooo',
    },
    'param2' => {
      'content' => 'even more of content for param2',
      'bar'     => 'bar bar bar',
    },
    'param3' => {
      'content' => 'some content for param3',
    },
  }
};

my $xml = XMLout($xmlHash, KeepRoot => 1);  

my $ua = LWP::UserAgent->new;
my $response = $ua->post('http://www.yourdomain.com/', Content => $xml);
if ($response->is_success) {
  print $response->decoded_content;  # or whatever
}
else {
  die $response->status_line;
}

您应该通读XML::Simple的文档,因为有时有些奇怪.

如果要解析也是XML的请求答复,则XML::Simple也可以用于此. XMLin()在这里起到了作用.


如果您要实现非常复杂的Web服务,请查看

The output is a hash but if it should return and id which is a no..where I am going wrong?

Depending on the nature of your request (size, content, how much does the structure depend on the input), there are several different ways to do this.

The most simple way would be to store your request inside a string in your program, put variables in and then send it.

#!/usr/bin/perl
use strict; use warnings;
use LWP::UserAgent;

my ($param1, $param2) = (1, 2);

my $xml = <<XMLREQUEST
<request>
  <param1>$param1</param1>
  <param2>$param2</param2>
</request>
XMLREQUEST
;

my $ua = LWP::UserAgent->new;
my $response = $ua->post('http://www.yourdomain.com/', Content => $xml);
if ($response->is_success) {
  print $response->decoded_content;  # or whatever
}
else {
  die $response->status_line;
}

In this case, we are using LWP::UserAgent to do the sending/receiving for us.

The next step would be to use a template engine. Text::Template can be used to do it. Our sample code with it looks like this:

#!/usr/bin/perl
use strict; use warnings;
use LWP::UserAgent;
use Text::Template;

my $vars = {
  'username' => 'jon',
  'password' => 'verysecure',
  'param1' => {
    'content' => 'a lot of content for param1',
    'foo'     => 'fofofofooo',
  },
  'param2' => {
    'content' => 'even more of content for param2',
    'bar'     => 'bar bar bar',
  },
  'param3' => {
    'content' => 'some content for param3',
  },
};

my $template = Text::Template->new(TYPE => 'FILEHANDLE', SOURCE => \*DATA );
my $xml = $template->fill_in(HASH => $vars);  

my $ua = LWP::UserAgent->new;
my $response = $ua->post('http://www.yourdomain.com/', Content => $xml);
if ($response->is_success) {
  print $response->decoded_content;  # or whatever
}
else {
  die $response->status_line;
}

__DATA__
<request>
  <auth>
    <username>{$username}</username>
    <password>{$password}</password>
  </auth>
  <param1 foo="{$param1{'foo'}}">{$param1{'content'}}</param1>
  <param2 bar="{$param2{'bar'}}">{$param2{'content'}}</param2>
  <param3>{$param3{'content'}}</param3>
</request>

It takes the $vars hashref and puts its content at the respective places in the template. $var->{'username'} is filled in at where the template says {$username}. The template in this case is read from the DATA section, which is specified below the program. A good way would be to have a template file for each request type you need to do. If the request contains optional elements, Text::Template can take care of this with conditional statements (putting Perl code in the template).

If you prefer a more dynamic approach, consider XML::Simple for easy tasks. Keep in mind that XML::Simple is not the best XML module around. There are others, like XML::Twig, that are more robust.

#!/usr/bin/perl
use strict; use warnings;
use LWP::UserAgent;
use XML::Simple;

my $xmlHash = {
  'request' => {
    'auth' => [ 
      {
        'username' => 'jon', 
      },
      {
        'password' => 'verysecure',
      },
    ],
    'param1' => {
      'content' => 'a lot of content for param1',
      'foo'     => 'fofofofooo',
    },
    'param2' => {
      'content' => 'even more of content for param2',
      'bar'     => 'bar bar bar',
    },
    'param3' => {
      'content' => 'some content for param3',
    },
  }
};

my $xml = XMLout($xmlHash, KeepRoot => 1);  

my $ua = LWP::UserAgent->new;
my $response = $ua->post('http://www.yourdomain.com/', Content => $xml);
if ($response->is_success) {
  print $response->decoded_content;  # or whatever
}
else {
  die $response->status_line;
}

You should read through the docs of XML::Simple because it is a bit strange sometimes.

If you want to parse the reply of your request which is also XML then XML::Simple can also be used for that. The XMLin() does the trick here.


If you have a very complicated web service that you want to implement, taking a look at SOAP::Lite or (if you have a large WSDL file) SOAP::WSDL.

这篇关于对于如何在Perl中发出XML请求-需要帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆