Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to send soap request to JAVA web services

Below is the create service in JAVA Axis. i need attach document in this soap request

XML
<soapenv:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:doc="http://document.service.servicelayer.dms.cmcc.st" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
  <soapenv:header>
  <soapenv:body>
        <doc:create soapenv:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
              <documenttype xsi:type="soapenc:string">st_work_instr
              <matrixname xsi:type="soapenc:string">c_02400_08_Work Instructions
              <ticket xsi:type="soapenc:string">E9F491E565777832CE105AF781F035E2C080C0D8094E9FBE
              <attrname xsi:type="cre:ArrayOf_xsd_anyType" 
="" soapenc:arraytype="xsd:anyType[]" xmlns:cre="https://dmsws-qa.sgp.st.com:20000/dmsservice/layer/CreateService">
                    <item xsi:type="xsd:string">st_category
                    <item xsi:type="xsd:string">title
                    <item xsi:type="xsd:string">r_object_type
                    <item xsi:type="xsd:string">st_approver
                    <item xsi:type="xsd:string">st_responsible
                    <item xsi:type="xsd:string">st_wi_department
                    <item xsi:type="xsd:string">st_business_domain
              
              <attrvalue xsi:type="cre:ArrayOf_xsd_anyType" 
="" soapenc:arraytype="xsd:anyType[]" xmlns:cre="https://dmsws-qa.sgp.st.com:20000/dmsservice/layer/CreateService">
                    <item xsi:type="xsd:string">08-01-01
                    <item xsi:type="xsd:string">seongtest01
                    <item xsi:type="xsd:string">st_work_instr
                    <item xsi:type="xsd:string">dmsupport1
                    <item xsi:type="xsd:string">dmsupport1
                    <item xsi:type="xsd:string">30
                    <item xsi:type="xsd:string">09
              
              <relchildnames xsi:type="cre:ArrayOf_xsd_anyType" 
="" soapenc:arraytype="xsd:anyType[]" xmlns:cre="https://dmsws-qa.sgp.st.com:20000/dmsservice/layer/CreateService">
              <relchildversions xsi:type="cre:ArrayOf_xsd_anyType" 
="" soapenc:arraytype="xsd:anyType[]" xmlns:cre="https://dmsws-qa.sgp.st.com:20000/dmsservice/layer/CreateService">
              <filename xsi:type="soapenc:string">Doc1.docx
              <relnames xsi:type="cre:ArrayOf_xsd_anyType" 
="" soapenc:arraytype="xsd:anyType[]" xmlns:cre="https://dmsws-qa.sgp.st.com:20000/dmsservice/layer/CreateService">
              <rendfilename xsi:type="soapenc:string">
              <rendtype xsi:type="soapenc:string">docx
              <historyfilename xsi:type="soapenc:string">
              <promoteoption xsi:type="soapenc:string">false


What I have tried:

C#
public void Upload(string uri, string filePath, string ticket, string docTitle, string userName)
        {
            var _action = "https://dmsws-qa.sgp.st.com:20000/dmsservice/layer/CreateService";
            string formdataTemplate = "Content-Disposition: form-data; filename=\"{0}\";Content-ID=\"Doc1.docx\";\r\nContent-Type: application/octet-stream\r\n\r\n";
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Headers.Add("SOAPAction", _action);
            request.Headers.Add("Accept-Encoding", "gzip,deflate");
            request.ServicePoint.Expect100Continue = false;
            request.Method = "POST";
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.Headers.Add("MIME-Version", "1.0");

            using(FileStream fileStream    = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                XmlDocument soapEnvelopeDocument = new XmlDocument();

                String _pathFiles = System.Configuration.ConfigurationManager.AppSettings.Get("XMLRequest");
                XmlDocument xmlFile = new XmlDocument();
                xmlFile.Load(_pathFiles + "CreateService.xml");
                string finalXML = xmlFile.InnerXml.Replace("?ticket", ticket).Replace("?docTitle", docTitle).Replace("?docApprover", userName);
                LoggerUtility.LogInfo(finalXML);
                //xmlFile.Save(_pathFiles + "ConnectService.xml");

                soapEnvelopeDocument.LoadXml(finalXML);
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(boundarybytes, 0, boundarybytes.Length);
                    string formitem = string.Format(formdataTemplate, Path.GetFileName(filePath));   
                    byte[] formbytes = Encoding.UTF8.GetBytes(formitem);
                    requestStream.Write(formbytes, 0, formbytes.Length);
                    byte[] buffer = new byte[1024 * 4];
                    int bytesLeft = 0;

                    while ((bytesLeft = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        requestStream.Write(buffer, 0, bytesLeft);
                    }
                    StreamReader stmreader = new StreamReader(requestStream);
                    string webexception = stmreader.ReadToEnd();
                    soapEnvelopeDocument.Save(requestStream);
                }

            }

            try
            {           
                using (HttpWebResponse response    = (HttpWebResponse)request.GetResponse()) { }

                Console.WriteLine ("Success");
            }
            catch (WebException webex)
            {
                WebResponse errResp = webex.Response;
                using (Stream respStream = errResp.GetResponseStream())
                {
                    StreamReader webexceptionreader = new StreamReader(respStream);
                    string webexception = webexceptionreader.ReadToEnd();

                    LoggerUtility.LogException("DMSCreateServiceRequestError", webexception);

                    throw;
                }
            }
        }
Posted
Updated 7-Jan-20 17:10pm
v2
Comments
Richard MacCutchan 8-Jan-20 3:37am    
And? Do you have a question?
Member 14676278 8-Jan-20 4:15am    
yes, how to write in C# for the code
Richard MacCutchan 8-Jan-20 4:19am    
Sorry, that does not mean anything. Please edit your question and explain exactly what the problem is, and where it occurs.
Member 14676278 8-Jan-20 4:50am    
My question is on the subject right? there is soap envelope message i need to send to JAVA web services. I have tried to wrote alot of code to send the soap envelope message with attachment file. but it is not working. so i post the sample soap envelope file and the code i have tried to write. but it does not mean anything for you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900