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. 【转】Pro Android学习笔记(一):Android 平台 2013.6.4

    本系列是阅读<Pro Android4>的读书笔记,也包括网络阅读资料的整理,以及个人心得. 由于智能手机引入AP(应用处理器),Android在某种意义上是个人计算机,具有桌面计算机的完 ...

  2. AR/VR-VR-Info-Micron-Insight:一镜观一屋:VR 将建筑设计变为现实

    ylbtech-AR/VR-VR-Info-Micron-Insight:一镜观一屋:VR 将建筑设计变为现实 1.返回顶部 1. 一镜观一屋:VR 将建筑设计变为现实 想象一下,在一栋为你设计的还没 ...

  3. android和iOS平台的崩溃捕获和收集

    转自:http://www.cnblogs.com/lancidie/archive/2013/04/13/3019349.html 通过崩溃捕获和收集,可以收集到已发布应用(游戏)的异常,以便开发人 ...

  4. 怎样用win7电脑做无线路由

    http://jingyan.baidu.com/article/574c5219e9abce6c8c9dc17b.html http://nokia.zol.com.cn/252/297_25106 ...

  5. 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)

    No valid host was found. There are not enough hosts available 'ascii' codec can't decode byte 0xe6 i ...

  6. PDF,IMAGE,HTML,WORD,EXCEL 互操作

    http://www.cnblogs.com/lyl6796910/p/3318056.html

  7. linux命令:gzip命令

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...

  8. Python学习笔记(随机数)

    random模块的作用是产生随机数. import random num = random.randint(1,100) random.randint(a, b)可以生成一个a到b间的随机整数,包括a ...

  9. visdom可视化pytorch训练过程

    一.前言 在深度学习模型训练的过程中,常常需要实时监听并可视化一些数据,如损失值loss,正确率acc等.在Tensorflow中,最常使用的工具非Tensorboard莫属:在Pytorch中,也有 ...

  10. maven scope 以及依赖传递

    https://www.cnblogs.com/mxm2005/p/4947905.html