HttpClient4使用简介(转) httpclient使用详解

http://hc.apache.org/httpcomponents-client/

HttpClient4使用简介(转) httpclient使用详解
lib:
apache-mime4j-0.6.jar
commons-codec-1.3.jar
commons-logging-1.1.1.jar
httpclient-4.0.2.jar
httpcore-4.0.1.jar
httpmime-4.0.2.jar

一个简单的Demo,包含proxy, authentication, post entity,属性设置。

Java代码HttpClient client = newDefaultHttpClient(); // set proxyHttpHost proxy = newHttpHost("someproxy", 8080); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,"user_agent"); // set authentication((DefaultHttpClient)client).getCredentialsProvider().setCredentials(newAuthScope("localhost", 443), newUsernamePasswordCredentials("username", "password"));// set forma ted url//List<NameValuePair> qparams = newArrayList<NameValuePair>();// qparams.add(newBasicNameValuePair("q", "httpclient"));// qparams.add(newBasicNameValuePair("btnG", "Google Search"));// qparams.add(newBasicNameValuePair("aq", "f")); // qparams.add(newBasicNameValuePair("oq", null));// URI uri =URIUtils.createURI("http", "www.google.com", -1, "/search",URLEncodedUtils.format(qparams, "UTF-8"), null);// HttpGet get = newHttpGet(uri); HttpPost post = newHttpPost("http://localhost/"); // determines the timeoutin milliseconds until a connection is established.post.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,1000L); // defines the sockettimeout (SO_TIMEOUT) in milliseconds, which is the timeout forwaiting for data or, put differently, a maximum period inactivitybetween two consecutive data packets).post.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,1000L); // set post method entityList<NameValuePair>nvps = newArrayList<NameValuePair>();nvps.add(newBasicNameValuePair("IDToken1", "username"));nvps.add(newBasicNameValuePair("IDToken2", "password"));post.setEntity(newUrlEncodedFormEntity(nvps, HTTP.UTF_8));HttpResponse response =client.execute(post); System.out.println(response.getStatusLine().getStatusCode());Header[] headers =response.getAllHeaders(); HttpEntity entity =response.getEntity(); System.out.println(entity.getContentType());System.out.println(entity.getContentLength());System.out.println(EntityUtils.toString(entity));entity.consumeContent();client.getConnectionManager().shutdown();http://jja1982.iteye.com/blog/765539

  

爱华网本文地址 » http://www.aihuau.com/a/25101015/254330.html

更多阅读

DM分区软件使用详解图文 dm硬盘分区工具

硬盘分区是我们日常维护中必须掌握的一个基本知识,随着大容量硬盘的出现,Fdisk的缺点逐渐显示在我们面前。在分区时,Fdisk已经不能对容量在120G以上的硬盘进行分区操作,而且在对大容量硬盘进行分区操作时,Fdisk的速度让我们难以忍受。DM

iOSUIAppearance使用详解 ios tag 值使用详解

iOS5及其以后提供了一个比较强大的工具UIAppearance,我们通过UIAppearance设置一些UI的全局效果,这样就可以很方便的实现UI的自定义效果又能最简单的实现统一界面风格,它提供如下两个方法。+(id)appearance这个方法是统一全部改,比如

转载 Tor使用详解 转载 嫁娶择日详解

原文地址:Tor使用详解作者:学无止境一.安装Vidalia下载完成后就可以开始安装了,首先双击下载的安装程序启动安装界面,点击”Next”开始安装(图1),如果你的系统中没有安装Firefox浏览器,软件还会提醒你安装Firefox(火狐)浏览器,如果你不想安

ddif=/dev/zeroof=的含义是什么?Linux下的dd命令使用详解 dd dev zero

一、dd命令的解释dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512;c=1;k=1024;w=2参数注释:1. if=文件名:输入文件名,缺省为标准输入。即指定源文件。< if=input fi

声明:《HttpClient4使用简介(转) httpclient使用详解》为网友情歌分享!如侵犯到您的合法权益请联系我们删除