java ntlm_Java访问基于NTLM协议的SharePoint List Web服务的示例!

import java.io.*;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.util.EntityUtils;importorg.apache.http.HttpHost;importorg.apache.http.auth.AuthScope;importorg.apache.http.auth.NTCredentials;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.protocol.BasicHttpContext;importorg.apache.http.protocol.HttpContext;importjava.util.Iterator;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;public classTest {/***@paramargs*/

public static voidmain(String[] args) {//TODO Auto-generated method stub

DefaultHttpClient httpclient = newDefaultHttpClient();

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 = newBasicHttpContext();//Execute a cheap method first. This will trigger NTLM authentication

HttpGet httpget = new HttpGet("http://portal.contoso.uat/sites/cockpit/_vti_bin/listdata.svc/自定义列表");//httpget.setHeader("accept", "application/json");

HttpResponse response1 = null;try{

response1=httpclient.execute(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:/自定义列表.xml");

saveFile("c:/自定义列表.xml",EntityUtils.toString(entity1),false);

SAXReader reader= newSAXReader();try{

Document document= reader.read(new File("c:/自定义列表.xml"));

Element rootElm=document.getRootElement();//枚举根节点下所有子节点

for (Iterator ie =rootElm.elementIterator(); ie.hasNext();)

{

Element 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()=="人员名称" || Xelement.getName()=="应发工资")

{

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) throwsIOException {

BufferedWriter bw= null;

OutputStreamWriter osw= null;

File f= newFile(file);

FileOutputStream fos= newFileOutputStream(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= newBufferedWriter(osw);if (data != null) bw.write(data);

}catch(IOException ex) {throwex;

}finally{try { bw.close(); fos.close(); } catch(Exception ex) { }

}

}/*** 删除文件

*@paramfilePathAndName String 文件路径及名称 如c:/file.xml

*@paramfileContent String

*@returnboolean*/

public static voiddelFile(String filePathAndName) {try{

String filePath=filePathAndName;

filePath=filePath.toString();

java.io.File myDelFile= newjava.io.File(filePath);

myDelFile.delete();

}catch(Exception e) {

System.out.println("删除文件操作出错");

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值