Go调用webservice

本文介绍了使用Go语言调用WebService的实践,作者在对比了Java、Go、Python后,认为Go在速度和编码效率上有优势,但在处理Oracle数据库方面不如Java。文中提供了一个Go调用WebService的示例,适合需要此功能的开发者参考。
摘要由CSDN通过智能技术生成

Go调用webservice

最近写项目,切换在java,go,python之间……不过也好,同时对三种语言进行了对比,给我的感觉就是基础原理最重要,只要明白了原理,编程语言只不过是工具罢了。从速度和编码量来说,go无疑是最好的选择,无奈项目中使用oracle数据库较多,只得做备选;java毋庸置疑还是首选,毕竟与项目贴合;python主要是用于做一些快速类的数据分析,以满足业务对报表需要。下面就是用go调用webservice的例子,如果要插入数据库进行保存,返回的数据库连接根据自身情况编写即可。

下面展示一些 内联代码片

// go and webservice
maple
// An highlighted block
/*
程序目的:查询起飞城市到目的城市的航班信息
程序版本:go1.14
程序编写:枫叶
编写时间:2020年3月15日
 */
package main

import (
	"GoAlgorithm/AirlineSchedule"
	"fmt"
)

const(
	airUrl = "http://ws.webxml.com.cn/webservices/DomesticAirline.asmx?wsdl"
	contentType = "application/soap+xml; charset=utf-8"
)

func main(){
   
	//定义起飞城市,抵达城市,查询时间
	startCity := "桂林"
	lastCity := "厦门"
	theDate := "2020-3-18"
	userID := ""
	fmt.Println("----------航班查询系统---------")
	AirlineSchedule.ReceiveParam(airUrl,contentType,startCity,lastCity,theDate,userID)
}

/*
程序目的:设置请求报文
程序版本:go1.14
程序编写:枫叶
编写时间:2020年3月15日
 */
package AirlineSchedule

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

//对外接口函数
func ReceiveParam(url string,contentType string,startCity string,lastCity string,theDate string,userID string){
   
	fmt.Println("-------正在查询",startCity,"到",lastCity,"航班信息-------")
	buildRequest(url,contentType,startCity,lastCity,theDate,userID)
}

//soap1.2
func buildRequest(url string,contentType string,startCity string,lastCity string,theDate string,userID string){
   
	req := `<?xml version="1.0" encoding="utf-8"?>
	<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
	<soap12:Body>
	<getDomesticAirlinesTime xmlns="http://WebXml.com.cn/">
	<startCity>`+sta
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 HTTP GET 方式调用 WebService 需要以下步骤: 1. 创建 WebService 引用:在 Visual Studio 中打开项目,右键单击“引用”,选择“添加服务引用”,在对话框中输入 WebService 的 URL 地址,然后单击“Go”按钮,等待 Visual Studio 自动检测并生成 WebService 的客户端代理代码。 2. 创建 WebRequest 对象:在代码中创建一个 WebRequest 对象,并设置其 Method 属性为 "GET"。 3. 添加请求参数:如果 WebService 需要传递参数,则可以将参数添加到请求 URL 中。例如:http://example.com/MyWebService.asmx/MyMethod?param1=value1&param2=value2。 4. 发送请求并获取响应:使用 WebRequest.GetResponse 方法发送请求并获取响应结果,响应结果包括 HTTP 状态码、响应头和响应正文。 下面是一个示例代码,演示如何使用 HTTP GET 方式调用 WebService: ```csharp string url = "http://example.com/MyWebService.asmx/MyMethod?param1=value1&param2=value2"; WebRequest request = WebRequest.Create(url); request.Method = "GET"; WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string responseText = reader.ReadToEnd(); Console.WriteLine(responseText); ``` 需要注意的是,使用 HTTP GET 方式调用 WebService 时,请求 URL 的长度是有限制的,一般不能超过 2048 个字符。如果需要传递大量数据,建议使用 HTTP POST 方式调用 WebService
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值