zoukankan      html  css  js  c++  java
  • C#调用Java的WebService添加SOAPHeader验证

    C#调用Java的WebService添加SOAPHeader验证(2)

    1.问题描述

    调用的Java的webservice

    string Invoke(string func, string reqXml)

     使用C#直接调用一直报错。

    webservice提供方有说明如下:

    身份验证采用对SOAP身份认证(用户名/密码验证/序列号)的方式部署,设定用户名和密码由系统配置,所有文本内容编码选择UTF-8编码规范
    <?xml version='1.0' encoding='utf-8'?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header>
    <vc:Authentication xmlns: vc ="http://ant.com">
    <vc:Username>[系统配置] </vc:Username>
    <vc:Password>[系统配置]</vc:Password>
    <vc:SerialNo>[系统配置]</vc:SerialNo >
    </vc:Authentication>
    </soapenv:Header>
    <soapenv:Body>
    </soapenv:Body>
    </soapenv:Envelope> 
    

    相信就是soapenv:Header这里的问题了,C# 没soapenv:Header这些东西的

    网上查了好多类似下面的

    https://www.cnblogs.com/o2ds/p/4093413.html

    C#访问Java的WebService添加SOAPHeader验证的问题

    都没有用

    后来尝试sopui及xmlspy创建soap,直接发送xml,终于试出来了,然后C#使用http post调用webservice,成功了。

    2.问题解决1

    C#拼接的需要http post的soap字符串如下

    </SOAP-ENV:Header> 照搬给的文档里的字符串
    <SOAP-ENV:Body> 为调用函数,string Invoke(string func, string reqXml) 函数名Invoke,两个参数,自行理解吧

    注意:里面的 换行标志都要保留,不然都是报错

     string soap = "<?xml version="1.0" encoding="utf-8"?>
    " +
                    "<SOAP-ENV:Envelope
    " +
                    "xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    " +
                    "xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    " +
                    "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    " +
                    "xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    " +
                    "<SOAP-ENV:Header>
    " +
                        "<vc:Authentication
    " +
                            "xmlns:vc="http://ant.com">
    " +
                            "<vc:Username>xx</vc:Username>
    " +
                            "<vc:Password>xxx</vc:Password>
    " +
                            "<vc:SerialNo>xxxx</vc:SerialNo>
    " +
                        "</vc:Authentication>
    " +
                    "</SOAP-ENV:Header>
    " +
                    "<SOAP-ENV:Body>
    " +
                        "<m:Invoke
    " +
                            "xmlns:m="http://tempuri.org/">
    " +
                            "<m:func>" + jkid + "</m:func>
    " +
                            "<m:reqXml>" + HttpUtility.HtmlEncode(xml) + "</m:reqXml>
    " +
                        "</m:Invoke>
    " +
                    "</SOAP-ENV:Body>
    " +
                    "</SOAP-ENV:Envelope>";
    

      然后发送,随便找个http post的代码就行了

     public static string GetSOAPReSource(string url, string datastr)
            {
                try
                {
                    //request
                    Uri uri = new Uri(url);
                    WebRequest webRequest = WebRequest.Create(uri);
                    webRequest.ContentType = "text/xml; charset=utf-8";
    
                    webRequest.Method = "POST";
                    using (Stream requestStream = webRequest.GetRequestStream())
                    {
                        byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
                        requestStream.Write(paramBytes, 0, paramBytes.Length);
                    }
                    //response
                    WebResponse webResponse = webRequest.GetResponse();
                    using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                    {
                        string result = "";
                        return result = myStreamReader.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

      

    3.问题解决2

    发现还是报错,http 500错误,和之前不一样,但依然不对

    研究webservice的wsdl发现了问题

    调用时加上

    webRequest.Headers.Add("SOAPAction", "http://tempuri.org/IAjsjService/Invoke");

    终于成功了
     public static string GetSOAPReSource(string url, string datastr)
            {
                try
                {
                    //request
                    Uri uri = new Uri(url);
                    WebRequest webRequest = WebRequest.Create(uri);
                    webRequest.ContentType = "text/xml; charset=utf-8";
                    webRequest.Headers.Add("SOAPAction", "http://tempuri.org/IAjsjService/Invoke");
    
                    webRequest.Method = "POST";
                    using (Stream requestStream = webRequest.GetRequestStream())
                    {
                        byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
                        requestStream.Write(paramBytes, 0, paramBytes.Length);
                    }
                    //response
                    WebResponse webResponse = webRequest.GetResponse();
                    using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                    {
                        string result = "";
                        return result = myStreamReader.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

      

    其他调用webservice的方式:

    C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用

  • 相关阅读:
    SolrCloud阶段总结
    Solr总结
    机器学习算法与Python实践之(六)二分k均值聚类
    机器学习问题方法总结
    浅谈Kmeans聚类
    AVL树的算法思路整理
    Solr4.6从数据库导数据的步骤
    红黑树
    浅谈 Adaboost 算法
    POJ 1836 Alignment
  • 原文地址:https://www.cnblogs.com/jhlong/p/10406652.html
Copyright © 2011-2022 走看看