Call with HttpClient:
Before you can invoke it, you need to authenticate through Windows ntml.
1 classWebServiceTest2 {3 4 /**5 * Get Interface Data6 * @paramsoaprequest7 * @returnString8 */9 Publicstring Postsoaprequest (String soaprequest) {TenCloseablehttpclient httpclient =Httpclients.createdefault (); One //Windows NTLM authentication ACredentialsprovider Credsprovider =NewBasiccredentialsprovider (); - credsprovider.setcredentials (Authscope.any, - Newntcredentials (Interfaceconstants.the_username, the Interfaceconstants.the_password, - Interfaceconstants.the_host, -System.getenv ("UserDomain"))); -Httphost target =NewHttphost (interfaceconstants.the_host, "http"); +Httpclientcontext context =httpclientcontext.create (); - Context.setcredentialsprovider (credsprovider); +HttpGet HttpGet =NewHttpGet (interfaceconstants.the_url); ACloseablehttpresponse response1 =NULL; at Try { -Response1 =Httpclient.execute (target, httpget, context); -}Catch(Exception e) { - e.printstacktrace (); -}finally { - Try { in response1.close (); -}Catch(IOException e) { to e.printstacktrace (); + } - } the //use the same context to perform a heavyweight approach *HttpPost HttpPost =NewHttpPost (interfaceconstants.the_url); $Httpentity re =NewStringentity (Soaprequest, "Utf-8"); Panax NotoginsengHttppost.setheader ("Content-type", "Text/xml; Charset=utf-8"); - httppost.setentity (re); theCloseablehttpresponse Response2 =NULL; +String result =NULL;//return Results A Try { theResponse2 =Httpclient.execute (target, httppost, context); +Httpentity Entity2 =response2.getentity (); - if(Entity2! =NULL) { $ //Response Content $result = Entityutils.tostring (Entity2, "Utf-8"); -System.out.println ("ischunked:" +entity2.ischunked ()); - } the}Catch(Exception e) { - e.printstacktrace ();Wuyi}finally { the Try { - response2.close (); Wu}Catch(IOException e) { - e.printstacktrace (); About } $ } - returnresult; - } - A + @Test the Public voidTesthelloworld ()throwsException { - /*SOAP Requests*/ $String soaprequest = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" the+ "<soap:envelope xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ "" the+ "xmlns:xsd=\" Http://www.w3.org/2001/xmlschema\ "" the+ "xmlns:soap=\" http://schemas.xmlsoap.org/soap/envelope/\ ">" the+ "<soap:Body>" -+ " in+ "</soap:Body>" the+ "</soap:Envelope>"; the AboutString res = postsoaprequest (soaprequest);//called the the /*processing the returned results*/ theDocument document =Documenthelper.parsetext (res); +Element root =document.getrootelement (); -Element BODY = root.element ("Body"); theElement Responseele = body.element ("Helloworldresponse");BayiElement Resultele = responseele.element ("Helloworldresult"); the System.out.println (Resultele.getdata ()); the - } - } the the the /**Interface Related Information*/ the Public Interfaceinterfaceconstants { - the Public Static FinalString The_url = "Http://xxxxxxxxervice.asmx"; the /** the * User name94 */ the Public Static FinalString the_username = "xxxxx"; the /** the * Password after encryption98 */ About Public Static FinalString The_password = "xxxxx"; - /**101 * Interface Host102 */103 Public Static FinalString the_host= "Xxxxxxxxxxxxx";104 /** the * Interface Address106 */107 Public Static FinalString the_url= "Http://xxxxxxxxervice.asmx";108 109}
HttpClient calling. NET published WebService with Windows ntml authentication