title: How to send SOAP request over HTTP use digest authentication HTTP in Java (or C#)?
body: <p>I  have web service with url is <a href="http://192.168.0.10/services/abc?wsdl" rel="nofollow noreferrer">http://192.168.0.10/services/abc?wsdl</a><br>
  This web server using digest authentication with username is admin and password is admin<br>
  I want to send request follow to this server</p>
  
  <p>SOAP request XML is SOAP_RQ.XML</p>
  
  <pre><code> &lt;soapenv:Envelope
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="LogsGet" xmlns:mal="MalteseGlobal" xmlns:job="JobGlobal"&gt;
           &lt;soapenv:Body&gt;
               &lt;log:LogsGetReq Cmd="Start" OpV="01.00.00" Sev="Info to critical"/&gt;
            &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;     
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Nc5xa.jpg" alt="Picture below description about SOAP communication over HTTP"><br>
  My code:</p>
  
  <pre><code> private static SOAPMessage createSOAPRequest(String username, String password) throws Exception {
          MessageFactory messageFactory = MessageFactory.newInstance();
          SOAPMessage soapMessage = messageFactory.createMessage();
          SOAPPart soapPart = soapMessage.getSOAPPart();
          // SOAP Envelope
          SOAPEnvelope envelope = soapPart.getEnvelope();
          envelope.addNamespaceDeclaration("log", "LogsGet");
          envelope.addNamespaceDeclaration("mal", "MalteseGlobal");
          envelope.addNamespaceDeclaration("job", "JobGlobal");
          // SOAP Body
          SOAPBody soapBody = envelope.getBody();
          SOAPElement soapBodyElem = soapBody.addChildElement("LogsGetReq", "log");
          QName Cmd = new QName("Cmd");
          QName OpV = new QName("OpV");
          QName Sev = new QName("Sev");
          soapBodyElem.addAttribute(Cmd, "Start");
          soapBodyElem.addAttribute(OpV, "01.00.00");
          soapBodyElem.addAttribute(Sev, "Info to critical");
          //SOAP Header
          MimeHeaders hd = soapMessage.getMimeHeaders();
          hd.addHeader("UsernameToken", username);
          hd.addHeader("PasswordText", password);
  
          soapMessage.saveChanges();       
          return soapMessage;
      }
  
  public void sendSoapRequest(String url, String username, String password) {
          try {
              // Create SOAP Connection
              SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
              SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  
              // Send SOAP Message to SOAP Server         
              SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(username, password, txtArea), url);
              // Process the SOAP Response               
              ByteArrayOutputStream bos = new ByteArrayOutputStream();
              soapResponse.writeTo(bos);
              System.out.println();  
              soapConnection.close();
  
          } catch (Exception e) {
              System.out.println("Error occurred while sending SOAP Request to Server");              
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>When I send request, I received a message below:
  Bad response: (401Authorization Required)</p>
  
  <p>If I send request use curl tool (<a href="http://curl.haxx.se/download/curl-7.41.0.zip" rel="nofollow noreferrer">http://curl.haxx.se/download/curl-7.41.0.zip</a>)  with
  command line:      curl.exe -X POST <a href="http://192.168.0.10/services/Maltese" rel="nofollow noreferrer">http://192.168.0.10/services/Maltese</a> -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction:LogsGet" --digest -u admin:admin -d @SOAP_RQ.xml -v<br>
  I received message response OK.</p>
  
  <p>Can any body help me, How to send SOAP request over HTTP use JAVA (or C#)?</p>
  
  <p>Thanks</p>