使用URL工具类调用webservice接口(soap)与http接口的实现方式

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class SoapTest {

public void testPost(String urlStr,String data) {
try {
//创建指定链接的url对象
URL url = new URL(urlStr);
//建立到url对象之间的链接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true
con.setDoOutput(true);
//设置第一次请求的数据内容不被存储
con.setRequestProperty("Pragma:", "no-cache");
//设置请求的数据内容不被存储
con.setRequestProperty("Cache-Control", "no-cache");
//设置请求的字符集编码格式,如果不指定,则默认采用16进制的字符集编码格式
con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
//构造向指定链接写入数据的的输出流
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
//获取xml数据
String xmlInfo = getXmlInfo(data);
//向指定链接写入数据
out.write(new String(xmlInfo));
out.flush();
out.close();
//将从服务端返回的数据读取到内存中
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
//构造一个空的StringBuffer对象,用于存储内存中的数据
StringBuffer buf = new StringBuffer();
for (line = br.readLine(); line != null; line = br.readLine()) {
//由于服务端返回的数据的字符集编码有可能不是utf-8,需要对返回的数据通过指定的字符集进行解码
buf.append(new String(line.getBytes(),"UTF-8"));
}

//获取服务端返回的HttpCode
  int httpCode = con.getResponseCode();
  System.out.println("HttpCode:"+httpCode+"  "+map.get("msgType")+"接口:"+buf.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//处理xml数据
public static String getXmlInfo(String data){
//对webservice接口进行加密(SHA-1加密)
SoapKey soapKey = new SoapKey();
String key=soapKey.getMessageDigest(data, "SHA-1");
//处理传输数据的"<"与">"字符
String str = new String(data.replace("<","&lt;")).replace(">","&gt;");
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.ws.gpo.yy.com/\">"+"<soapenv:Header/>"
+"<soapenv:Body><ser:sendRecv4Supplier><!--Optional:--><sUser>zhuwei</sUser><!--Optional:--><sPwd>password</sPwd><!--Optional:--><sJgbm>GYSNB001</sJgbm><!--Optional:--><sVersion>1.0.0.0</sVersion><!--Optional:--><sXxlx>GS102</sXxlx><!--Optional:--><sSign>"+key+"</sSign><!--Optional:-->"
+"<xmlData>"+str+"</xmlData>"
+"</ser:sendRecv4Supplier></soapenv:Body></soapenv:Envelope>";
return xml;
}

public static void main(String[] args) throws UnsupportedEncodingException {
String url = "http://192.168.1.208:8090/gpodec/webservice/supplierService";
new SoapTest().testPost(url);

}

转载于:https://www.cnblogs.com/zw520ly/p/5873933.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++中调用Web服务接口,可以使用SOAP协议,它是一种基于XML的协议,用于在不同的应用程序之间进行通信。以下是使用SOAP和C++调用Web服务的一般步骤: 1. 创建SOAP消息:使用SOAP协议创建需要发送到Web服务的XML消息。 2. 配置SOAP通信:设置Web服务的URLSOAP操作的名称。 3. 发送SOAP消息:将SOAP消息发送到Web服务,并接收响应。 4. 解析SOAP响应:将Web服务返回的SOAP响应解析为C++对象。 下面是一个简单的示例,演示如何使用C++和SOAP协议调用Web服务: ```c++ #include "soapH.h" #include "MyWebServiceSoapProxy.h" int main() { MyWebServiceSoapProxy service; ns1__Add addRequest; ns1__AddResponse addResponse; addRequest.a = 3; addRequest.b = 5; if (service.Add(&addRequest, &addResponse) == SOAP_OK) { std::cout << "The sum of 3 and 5 is: " << addResponse.AddResult << std::endl; } else { service.soap_stream_fault(std::cerr); } return 0; } ``` 在上面的代码中,我们使用了gSOAP工具自动生成的MyWebServiceSoapProxy类,它包含了所有我们需要调用Web服务的方法和数据结构。在本示例中,我们调用了名为Add的Web服务方法,并传递了两个整数作为参数。如果调用成功,我们将在控制台上输出它们的和。 请注意,此示例仅为演示如何使用SOAP和C++调用Web服务。实际上,SOAP协议有时可能会变得过于冗长和复杂,因此可以考虑使用其他协议,如REST或JSON-RPC。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值