package main   

import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
) // The URL of the SOAP server
const MH_SOAP_URL = "http://sp.mountyhall.com/SP_WebService.php" // this is just the message I'll send for interrogation, with placeholders
// for my parameters
const SOAP_VUE_QUERY_FORMAT = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:tns=\"urn:SP_WebService\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" ><SOAP-ENV:Body><mns:Vue xmlns:mns=\"uri:mhSp\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><numero xsi:type=\"xsd:string\">%d</numero><mdp xsi:type=\"xsd:string\">%s</mdp></mns:Vue></SOAP-ENV:Body></SOAP-ENV:Envelope>" // Here I define Go structures, almost identical to the structure of the
// XML message we'll fetch
// Note that annotations (the string "return>item") allow to have a slightly
// different structure or different namings type SoapItem struct {
Numero int
Nom string
Type string
PositionX int
PositionY int
PositionN int
Monde int
}
type SoapVue struct {
Items []SoapItem "return>item"
}
type SoapFault struct {
Faultstring string
Detail string
}
type SoapBody struct {
Fault SoapFault
ProfilResponse SoapProfil
VueResponse SoapVue
}
type SoapEnvelope struct {
XMLName xml.Name
Body SoapBody
} // Here is the function querying the SOAP server
// It returns the whole answer as a Go structure (a SoapEnvelope)
// You could also return an error in a second returned parameter
func GetSoapEnvelope(query string, numero int, mdp string) (envelope *SoapEnvelope) {
soapRequestContent := fmt.Sprintf(query, numero, mdp)
httpClient := new(http.Client)
resp, err := httpClient.Post(MH_SOAP_URL, "text/xml; charset=utf-8", bytes.NewBufferString(soapRequestContent))
if err != nil {
// handle error
}
b, e := ioutil.ReadAll(resp.Body) // probably not efficient, done because the stream isn't always a pure XML stream and I have to fix things (not shown here)
if e != nil {
// handle error
}
in := string(b)
parser := xml.NewDecoder(bytes.NewBufferString(in))
envelope = new(SoapEnvelope) // this allocates the structure in which we'll decode the XML
err = parser.DecodeElement(&envelope, nil)
if err != nil {
// handle error
}
resp.Body.Close()
return
}

  

go语言简单的soap调用方法的更多相关文章

  1. DLL简单分析与调用方法

    最近为了分析一个没有代码的DLL有哪些函数,找了各种方法. 把结果分享一下:三个方法都没法得到函数的参数,有点让我失望. DLL Export Viewer NikPEViewer Dumpbin 配 ...

  2. onvif规范的实现:使用gSOAP创建SOAP调用实例

    预备知识 ONVIF规范中设备管理和控制部分所定义的接口均以Web Services的形式提供.ONVIF规范涵盖了完全的XML及WSDL的定义.每一个支持ONVIF规范的终端设备均须提供与功能相应的 ...

  3. R语言:用简单的文本处理方法优化我们的读书体验

    博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html 前言 延续之前的用R语言读琅琊榜小说,继续讲一下利用R语言做一些简单的文本处理.分词的事情.其实 ...

  4. 简单实现RN调用原生方法(IOS)

    在React Native中,一个“原生模块”就是一个实现了“RCTBridgeModule”协议的Objective-C类(个人理解RCTBridgeModule就是react与native之间的桥 ...

  5. 【Pyqt5】自定义信号简单原理(易懂版),多窗口交互,传输数据,调用方法

    PS:如果你想在2窗口调用1窗口的内部方法,或者在2窗口传递数据给1窗口数据,本片博客可以放心食用 主窗口: class MainWindow(QWidget,Ui_MainFrom): insert ...

  6. WebService服务调用方法介绍

    1 背景概述 由于在项目中需要多次调用webservice服务,本文主要总结了一下java调用WebService常见的6种方式,即:四种框架的五种调用方法以及使用AEAI ESB进行调用的方法. 2 ...

  7. PHP5下SOAP调用实现过程

    本文以某公司iPhone 6手机预约接口开发为例,介绍PHP5下SOAP调用的实现过程. 一.基础概念 SOAP(Simple Object Access Protocol )简单对象访问协议是在分散 ...

  8. PHP5下WSDL,SOAP调用实现过程

    一.基础概念 SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协议,它包括四个部分:SOAP封装 ...

  9. PHP使用SOAP调用.net的WebService数据

    需要和一个.net系统进行数据交换,对方提供了一个WebService接口,使用PHP如何调用这个数据呢,下面就看看使用SOAP调用的方法吧 这个与一般的PHP POST或GET传值再查库拿数据的思路 ...

随机推荐

  1. vijos:P1155集合位置(次短路)

    描述 每次有大的活动,大家都要在一起“聚一聚”,不管是去好乐迪,还是避风塘,或者汤姆熊,大家都要玩的痛快.还记得心语和花儿在跳舞机上的激情与释放,还记得草草的投篮技艺是如此的高超,还记得狗狗的枪法永远 ...

  2. python3 + selenium + eclipse 中报错:'geckodriver' executable needs to be in PATH

    Windows系统解决办法如下: 1.下载geckodriver.exe: 下载地址:https://github.com/mozilla/geckodriver/releases 请根据系统版本选择 ...

  3. Entity Framework之领域驱动设计实践

    http://www.cnblogs.com/daxnet/archive/2010/11/02/1867392.html

  4. ie8兼容rgba的方法

    现在做个网页还得考虑ie8,只想说:尼玛! 但是没办法,屈于淫威也得弄. 首先说下rgba的含义吧,rgba,r代表red,g代表green,b代表blue,a代表透明度. filter:progid ...

  5. angular学习的一些Mark

    http://www.cnblogs.com/xianrongbin/p/4104596.html http://angular-ui.github.io/

  6. CF-845C

    C. Two TVs time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  7. HDOJ-2153

    仙人球的残影 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. go开发环境搭建及开发工具简介

    go语言包的下载地址:https://www.golangtc.com/download 这里以window10的操作系统环境为例 go的开发工具下载地址:https://www.golangtc.c ...

  9. HDU - 5950 Recursive sequence(二项式+矩阵合并+矩阵快速幂)

    Recursive sequence Farmer John likes to play mathematics games with his N cows. Recently, they are a ...

  10. IT兄弟连 Java语法教程 Java平台的版本划分

    自从Sun公司推出Java以来,就力图使之无所不能.Java发展至今,按应用范围划分为3个版本,即Java SE.Java EE和Java ME,也就是SunOne(Open Net Environm ...