Recently, it was found that the httpclient of Java has supported the NTLM protocol since version 4.1. Earlier versions do not support the NTLM protocol (but can be implemented through open-source Jcifs library implementation) . Since other systems (Java) in the project need to call the Sharepoint list web service based on the NTLM protocol, an open source HTTP component is used before.The jcifs library implements simulated identity authentication. The URL is as follows: http://jcifs.samba.org/src/. in this case, you can access the access service of a normal access point.
This article mainly introducesCodeMore concise, easier to use based on the latest version of httpclient 4.2.5, the component download URL is as follows: http://hc.apache.org/downloads.cgi,
Such as JavaProgramTo access the custom list service of SharePoint, view the XML of this list in the browser, for example:
It should be noted that the XML encoding method is in UTF-8 format, such as using dom4j to parse this XML also requires special processing, ensure that the file header is in UTF-8 format, this format is not used by default. The complete Java sample code is as follows:
Import Java. Io .* ; Import Org. Apache. http. httpentity; Import Org. Apache. http. httpresponse; Import Org. Apache. http. Client. Methods. httpget; Import Org. Apache. http. impl. Client. defaulthttpclient; Import Org. Apache. http. util. entityutils; Import Org. Apache. http. httphost; Import Org. Apache. http. Auth. authscope; Import Org. Apache. http. Auth. ntcredentials; Import Org. Apache. http. Client. clientprotocolexception; Import Org. Apache. http. Protocol. basichttpcontext; Import Org. Apache. http. Protocol. httpcontext; Import Java. util. iterator; Import Org. dom4j. Document; Import Org. dom4j. element; Import Org. dom4j. Io. saxreader; Public Class Test { /** * @ Param ARGs */ Public Static Void Main (string [] ARGs ){ // Todo auto-generated method stub Defaulthttpclient httpclient = New Defaulthttpclient (); ntcredentials creds = New Ntcredentials ("usrname", "password", "my", "contosouat" ); Httpclient. getcredentialsprovider (). setcredentials (authscope. Any, creds); httphost target = New Httphost ("portal. contoso. UAT", 80, "HTTP" ); // Make sure the same context is used to execute logically related requests Httpcontext localcontext = New Basichttpcontext (); // Execute a cheap method first. This will trigger NTLM Authentication Httpget = New Httpget ("http://portal.contoso.uat/sites/cockpit/_vti_bin/listdata.svc/self-defined list" ); // Httpget. setheader ("accept", "application/JSON "); Httpresponse response1 =Null ; Try {Response1 = Httpclient.exe cute (target, httpget, localcontext ); // System. Out. Print (response1.tostring ()); } Catch (Clientprotocolexception e ){ // Todo auto-generated Catch Block E. printstacktrace ();} Catch (Ioexception e ){ // Todo auto-generated Catch Block E. printstacktrace ();} httpentity entity1 = Response1.getentity (); Try {Delfile ( "C:/custom list. xml" ); SaveFile ( "C:/custom list. xml", entityutils. tostring (entity1 ), False ); Saxreader Reader =New Saxreader (); Try {Document = Reader. Read ( New File ("C:/custom list. xml" ); Element rootelm = Document. getrootelement (); // Enumerate all subnodes under the root node For (Iterator Ie =Rootelm. elementiterator (); ie. hasnext ();) {element = (Element) IE. Next (); If (Element. getname () = "entry" ){ For (Iterator Ic = Element. elementiterator (); IC. hasnext ();) {element celement = (Element) IC. Next (); If (Celement. getname () = "content"){ For (Iterator IP = Celement. elementiterator (); IP. hasnext ();) {element pelement = (Element) ip. Next (); For (Iterator IX = Pelement. elementiterator (); ix. hasnext ();) {element xelement =(Element) Ix. Next (); If (Xelement. getname () = "Personnel name" | xelement. getname () = "pay-as-you-go" ) {System. Out. println (xelement. getname () + ":" + Xelement. getdata ());}} // System. Out. println (pelement. getname ()); } // System. Out. println (celement. getname ()); }}}}} Catch (Exception e ){ // Todo auto-generated Catch Block E. printstacktrace ();}} Catch (Exception e ){ // Todo auto-generated Catch Block E. printstacktrace ();}} Public Static Void SaveFile (string file, string data, Boolean Append) Throws Ioexception {bufferedwriter BW = Null ; Outputstreamwriter OSW =Null ; File F = New File; fileoutputstream fos = New Fileoutputstream (F, append ); Try { // Write utf8 BOM mark if file is empty If (F. Length () <1 ){ Final Byte [] BOM = New Byte [] {( Byte ) 0xef ,( Byte ) 0xbb ,( Byte ) 0xbf }; FOS. Write (BOM);} OSW = New Outputstreamwriter (FOS, "UTF-8" ); BW = New Bufferedwriter (OSW ); If (Data! = Null ) BW. Write (data );} Catch (Ioexception ex ){ Throw Ex ;} Finally { Try {BW. Close (); FOS. Close ();} Catch (Exception ex ){}}} /** * Delete an object * @ Param Filepathandname string file path and name such as C:/file. xml * @ Param Filecontent string * @ Return Boolean */ Public Static Void Delfile (string filepathandname ){ Try {String filepath = Filepathandname; filepath =Filepath. tostring (); Java. Io. File mydelfile = New Java. Io. File (filepath); mydelfile. Delete ();} Catch (Exception e) {system. Out. println ( "An error occurred while deleting the file" ); E. printstacktrace ();}}}
The code execution result is as follows:
After performing the preceding steps, you can access the list of SharePoint instances and parse the data properly. You can further expand and improve the Code to meet your actual needs.
| This blog for the original software life, welcome to reprint, reprint please indicate the source: http://www.cnblogs.com/nbpowerboy/archive/2013/05/19/3086579.html. Assume or use it for commercial purposes, but you must keep the signed software life (including links) in this article ). If you have any questions or authorization negotiation, please leave a message for me. |