【问题标题】:consume java servlet through C# asp.net where input and output is xml通过 C# asp.net 使用 java servlet,其中输入和输出是 xml
【发布时间】:2018-03-20 07:41:23
【问题描述】:

我有一个需要从 dot net 应用程序调用的 java servlet 服务。我找到了一些代码,但在响应流中我收到了 null。当通过 html 调用时它工作正常。

下面是 html 代码,它在执行时工作,xml 结构化数据被粘贴到文本框中并被调用。

<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></head><body>  
<form action="http://172.18.1.57:8080/webdesktop/APIExecutor" method="POST">  

<textarea rows="10" cols="150" name="inXml"></textarea>
<br><br>  
<input type="submit" value="register">  

</form>  

  </body></html>

以下是我在 xml 中的输入

<?xml version="1.0"?>
<WMConnect_Input>
<Option>WMConnect</Option>
<EngineName>abcuat</EngineName>
<ApplicationInfo>172.18.1.57</ApplicationInfo>
<Participant>
    <Name>username</Name>
    <Password>test1234</Password>
    <Scope></Scope>
    <UserExist>Y</UserExist>
    <Locale>en-US</Locale>
    <ParticipantType>U</ParticipantType>
</Participant>
</WMConnect_Input>

在xml中输出如下。

这适用于 html,但从 dot net 应用程序会抛出空值。

下面的代码我试过了。

click here to check code i have tried.

提前谢谢你。

【问题讨论】:

    标签: java c# asp.net xml servlets


    【解决方案1】:

    HttpWebRequest 会帮助你。放置适当的 Catch 块并获取异常以进一步过滤它。

                string myurl = uri + uriTemplate;
                string soapAction = string.Empty;
                String Method = methodType;//Define your method
                try
                {
                    XmlDocument soapEnvelopeXml = new XmlDocument();
                    var strSOAPXML = (Y)obj;//obj would be your request object
                    soapEnvelopeXml.LoadXml(Convert.ToString(strSOAPXML));  
                    HttpWebRequest webRequest=(HttpWebRequest)WebRequest.Create(uri);
                    webRequest.Headers.Add("SOAPAction", soapAction + uriTemplate);
                    webRequest.ContentType = "text/xml;charset=\"utf-8\"";
                    webRequest.Accept = "text/xml";
                    webRequest.Method = Method.ToUpper();                
                    webRequest.KeepAlive = false;
                    using (Stream stream = webRequest.GetRequestStream())
                    { soapEnvelopeXml.Save(stream); }
                    // begin async call to web request.
                    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
                    // suspend this thread until call is complete
                    asyncResult.AsyncWaitHandle.WaitOne();
                    // get the response from the completed web request.
                    string soapResult;                
                    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {                    
                        soapResult = rd.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                   throw ex;
                }
                return _result;
    

    【讨论】:

    • 我已经尝试过了,但仍然返回 null,我也尝试了 Soap UI,它给出了 null 响应,现在将与供应商的开发人员核实。谢谢您的代码。
    • 请确保来自具有有效请求和端点@Rujj 的 SOAP UI 的正确响应
    • bluescorpX ,我已经与供应商核实过,所以他们说 servlet 不是服务,所以你不能从soap ui 或邮递员调用,它需要通过发布请求从 web /html 调用。
    【解决方案2】:

    未指定参数名称且内容类型/mime 不同。

    内容类型为 - application/x-www-form-urlencoded

    以下是对我有用的代码。

    >   string URI = "http://172.18.1.57:8080/webdesktop/APIExecutor";
    >                 string myParameters = "inXml=<?xml version='1.0'?><WMConnect_Input><Option>WMConnect</Option><EngineName>uat</EngineName><ApplicationInfo>172.18.1.57</ApplicationInfo><Participant><Name>test1234</Name><Password>test1234</Password>    <Scope></Scope> <UserExist>Y</UserExist>    <Locale>en-US</Locale>  <ParticipantType>U</ParticipantType></Participant></WMConnect_Input>";
    > 
    > 
    >                 HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(URI);
    >                 req.Method = "POST";
    >                 req.ContentType = "application/x-www-form-urlencoded";
    >                 byte[] bytes = System.Text.Encoding.ASCII.GetBytes(myParameters);
    >                 req.ContentLength = bytes.Length;
    >                 System.IO.Stream os = req.GetRequestStream();
    >                 os.Write(bytes, 0, bytes.Length); //Push it out there
    >                 os.Close();
    >                 using (HttpWebResponse response = req.GetResponse() as System.Net.HttpWebResponse)
    >                 {
    >                     using (Stream st = response.GetResponseStream())
    >                     {
    >                         StreamReader str = new StreamReader(st, System.Text.Encoding.UTF8);
    >                         var res = str.ReadToEnd();
    >                     }
    >                 }
    

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多