httpurlconnection.setRequestProperty("Keep-Alive",CommonValues.Keep_Alive);
httpurlconnection.setConnectTimeout(CommonValues.ConnectionTimeOut);
httpurlconnection.setReadTimeout(CommonValues.ReadTimeOut);
httpurlconnection.connect();
int responsecode = httpurlconnection.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK) //对应HTTP响应中状态行的响应码{
//操作请求流,这里对应HTTP响应中的响应正文
}
if (httpurlconnection != null)
{
httpurlconnection.disconnect();
}
六、HttpURLConnection和HttpClient
1.概念
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能:HttpURLConnection。但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。
除此之外,在Android中,androidSDK中集成了Apache的HttpClient模块,用来提供高效的、最新的、功能丰富的支持 HTTP 协议工具包,并且它支持 HTTP 协议最新的版本和建议。使用HttpClient可以快速开发出功能强大的Http程序。
2.区别
HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等,
HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便,比如重访问的自定义,以及一些高级功能等。
URLConnection
HTTPClient
Proxies and SOCKS
Full support in Netscape browser, appletviewer, and applications (SOCKS: Version 4 only); no additional limitations from security policies.
Full support (SOCKS: Version 4 and 5); limited in applets however by security policies; in Netscape can't pick up the settings from the browser.
Authorization
Full support for Basic Authorization in Netscape (can use info given by the user for normal accesses outside of the applet); no support in appletviewer or applications.
Full support everywhere; however cannot access previously given info from Netscape, thereby possibly requesting the user to enter info (s)he has already given for a previous access. Also, you can add/implement additional authentication mechanisms yourself.
Methods
Only has GET and POST.
Has HEAD, GET, POST, PUT, DELETE, TRACE and OPTIONS, plus any arbitrary method.
Headers
Currently you can only set any request headers if you are doing a POST under Netscape; for GETs and the JDK you can't set any headers.
Under Netscape 3.0 you can read headers only if the resource was returned with a Content-length header; if no Content-length header was returned, or under previous versions of Netscape, or using the JDK no headers can be read.
Allows any arbitrary headers to be sent and received.
Automatic Redirection Handling
Yes.
Yes (as allowed by the HTTP/1.1 spec).
Persistent Connections
No support currently in JDK; under Netscape uses HTTP/1.0 Keep-Alive's.
Supports HTTP/1.0 Keep-Alive's and HTTP/1.1 persistence.
Pipelining of Requests
No.
Yes.
Can handle protocols other than HTTP
Theoretically; however only http is currently implemented.
No.
Can do HTTP over SSL (https)
Under Netscape, yes. Using Appletviewer or in an application, no.
No (not yet).
Source code available
No.
Yes.
3.案例
URLConnection
[java] String urlAddress = "http://192.168.1.102:8080/AndroidServer/login.do";
URL url;
HttpURLConnection uRLConnection;
public UrlConnectionToServer(){
} //向服务器发送get请求
public String doGet(String username,String password){
String getUrl = urlAddress + " username="+username+"&password="+password;
try {
url = new URL(getUrl);
uRLConnection = (HttpURLConnection)url.openConnection();