HttpClient calling. NET published WebService with Windows ntml authentication

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.