Send XML to the WebService of jwsdp and. net in the form of soap messages through HTTP requests.

Source: Internet
Author: User
Document directory
  • Send XML to the WebService of jwsdp and. net in the form of soap messages through HTTP requests.
Send XML to the WebService of jwsdp and. net in the form of soap messages through HTTP requests.

This section describes a code template that transmits soap messages to jwsdp's WebService (document/literal) in 'manually 'mode. The same method will be used to access infobel WebService.


1. XML requests to jwsdp WebService through HTTP

It is easier to use python to send a file to the web server. For jwsdp WebService, do not forget to add content-type to the message header. Only in this way can the call be received by the server. The following is a command line example where soap responses are printed without parsing. As the returned data is relatively complex, I will explain how to use pyxml to parse soap messages in the infobel web service example later.

 

Example 3. How to Use python to transmit soap messages

In the following example, we access readls (), which carries a string type return value.

# Submit XML soap messages in post Mode
Import sys, httplib
 
# A "as lighter as possible" SOAP message:
Sm_template = "" <? XML version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV: Envelope
SOAP-ENV: encodingstyle = "http://schemas.xmlsoap.org/soap/encoding"

Xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/">
SOAP-ENV: Body>
<NS1: readls xmlns: NS1 = "http://phonedirlux.homeip.net/types">
<String_1> % S </string_1>
</NS1: readls>
SOAP-ENV: Body>
SOAP-ENV: envelope>
"""
 
Soapmessage = sm_template % ("your message or e-mail ")
 
Print soapmessage
 
# Construct and send a message header
WebService = httplib. HTTP ("www. pascalbotte. Be ")
WebService. putrequest ("Post", "/rcX-WS/rcX ")
WebService. putheader ("host", "www. pascalbotte. Be ")
WebService. putheader ("User-Agent", "Python Post ")
WebService. putheader ("Content-Type", "text/XML; charset =/" UTF-8 /"")
WebService. putheader ("Content-Length", "% d" % Len (soapmessage ))
WebService. putheader ("soapaction ","/"/"")
WebService. endheaders ()
WebService. Send (soapmessage)
 
# Getting back
Statuscode, statusmessage, header = WebService. getreply ()
Print "response:", statuscode, statusmessage
Print "headers:", Header
Res = WebService. GetFile (). Read ()
Print res

If you want to use my WebService to test this code, click here (if it is online ). Generally, you can use your favorite development tool to create a client and modify the SOAP message to access other WebServices to capture the SOAP message exchange between the client and the server.

2. Use the pyxml Dom to send a SOAP message request to WebService.

How to post dynamic soap messages? The following example shows how to use the DOM of pyxml to create a SOAP message:

# Post an XML file and use Dom to create a SOAP message
Import sys, httplib
 
From XML. Dom import implementation
From XML. Dom. Ext import prettyprint
 
Import stringio
 
# Define the required namespace
EC = "http://schemas.xmlsoap.org/soap/encoding"
Soapenv = "http://schemas.xmlsoap.org/soap/envelope"
Myns = "http://phonedirlux.homeip.net/types"
 
# Dom documentation
Domdoc = implementation. createdocument (none, '', none)
 
# Soap encapsulation namespace
Seobj = domdoc. createelementns (soapenv, "SOAP-ENV: envelope ")
Seobj. setattributens (soapenv, "SOAP-ENV: encodingstyle", EC)
 
# Add to root
Domdoc. appendchild (seobj)
 
Header = domdoc. createelement ("SOAP-ENV: Header ")
Seobj. appendchild (header)
Body = domdoc. createelement ("SOAP-ENV: Body ")
Readls = domdoc. createelementns (myns, "NS1: readls ")
String_1 = domdoc. createelement ("string_1 ")
String_1.appendchild (domdoc. createtextnode ("message created with pyxml, your e-mail "))
Readls. appendchild (string_1)
Body. appendchild (readls)
 
Seobj. appendchild (Body)
 
Soapstr = stringio. stringio ()
Prettyprint (domdoc, soapstr)
 
# Viewing soap messages
Print soapstr. getvalue ()
 
# Construct a header and deliver it
WebService = httplib. HTTP ("www. pascalbotte. Be ")
WebService. putrequest ("Post", "/rcX-WS/rcX ")
WebService. putheader ("host", "www. pascalbotte. Be ")
WebService. putheader ("User-Agent", "my post ")
WebService. putheader ("Content-Type", "text/XML; charset =/" UTF-8 /"")
WebService. putheader ("Content-Length", "% d" % Len (soapstr. getvalue ()))
WebService. putheader ("soapaction ","/"/"")
WebService. endheaders ()
WebService. Send (soapstr. getvalue ())
 
# Getting back
Statuscode, statusmessage, header = WebService. getreply ()
Print "response:", statuscode, statusmessage
Print "headers:", Header
Res = WebService. GetFile (). Read ()
Print res

 

From: http://hi.baidu.com/moatlzy/blog/item/410fe43b5a12ebf83a87ce99.html

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.