简体   繁体   中英

consume java servlet through C# asp.net where input and output is xml

I have a java servlet service which need to be invoked from dot net application. I have found some code but in response stream i am receiving null.Where as when invoked through html it is working fine.

Below is html code which is working when executed and xml structured data is pasted in textbox and invoked.

<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>

below is my input in 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>

and output is as below in xml.

在此处输入图片说明

this works for html but from dot net application it throws null value.

below code i have tried .

click here to check code i have tried .

Thank you in advance.

HttpWebRequest would help you. Put proper Catch Block and get the exception to filter it further.

            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;

THe parameter name was not specified and content-type/mime was different.

content type as - application/x-www-form-urlencoded

Below is code which worked for me.

>   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();
>                     }
>                 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM