1将访问的路径转换成URL。
URL url =
new
URL(path);
2,通过URL获取连接。
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3,设置请求方式
conn.setRequestMethod("GET");
4,设置连接超时时间
conn.setConnectTimeout(
5000
);
5,设置读取时间
conn.setConnectTimeout(5000);
6,设置请求头的信息。
conn.setRequestProperty(User-Agent, Mozilla/
5.0
(compatible; MSIE
9.0
; Windows NT
6.1
; Trident/
5.0
));
7,获取响应码
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream
is = conn.getInputStream();
}
8,请求码200,表明请求成功,获取返回内容的输入流
/**
* 将输入流转换成字符串
*
* @param is
* 从网络获取的输入流
* @return
*/
public
static
String streamToString(InputStream is) {
try
{
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[
1024
];
int
len =
0
;
while
((len = is.read(buffer)) != -
1
) {
baos.write(buffer,
0
, len);
}
baos.close();
is.close();
byte
[] byteArray = baos.toByteArray();
return
new
String(byteArray);
}
catch
(Exception e) {
Log.e(tag, e.toString());
return
null
;
}
}
HttpUrlConnection
/**
* 通过HttpUrlConnection发送GET请求
*
* @param username
* @param password
* @return
*/
public
static
String loginByGet(String username, String password) {
String path = http:
//192.168.0.107:8080/WebTest/LoginServerlet?username= + username + &password= + password;
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5000
);
conn.setRequestMethod(GET);
int
code = conn.getResponseCode();
if
(code ==
200
) {
InputStream is = conn.getInputStream();
// 字节流转换成字符串
return
StreamTools.streamToString(is);
}
else
{
return
网络访问失败;
}
}
catch
(Exception e) {
e.printStackTrace();
return
网络访问失败;
}
/**
* 通过HttpUrlConnection发送POST请求
*
* @param username
* @param password
* @return
*/
public
static
String loginByPost(String username, String password) {
String path = http:
//192.168.0.107:8080/WebTest/LoginServerlet;
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5000
);
conn.setRequestMethod(POST);
conn.setRequestProperty(Content-Type, application/x-www-form-urlencoded);
String data = username= + username + &password= + password;
conn.setRequestProperty(Content-Length, data.length() + );
// POST方式,其实就是浏览器把数据写给服务器
conn.setDoOutput(
true
);
// 设置可输出流
OutputStream os = conn.getOutputStream();
// 获取输出流
os.write(data.getBytes());
// 将数据写给服务器
int
code = conn.getResponseCode();
if
(code ==
200
) {
InputStream is = conn.getInputStream();
return
StreamTools.streamToString(is);
}
else
{
return
网络访问失败;
}
}
catch
(Exception e) {
e.printStackTrace();
return
网络访问失败;
}
}
HttpClient
/**
* 通过HttpClient发送GET请求
*
* @param username
* @param password
* @return
*/
public
static
String loginByHttpClientGet(String username, String password) {
String path = http:
//192.168.0.107:8080/WebTest/LoginServerlet?username=
+ username + &password= + password;
HttpClient client =
new
DefaultHttpClient();
// 开启网络访问客户端
HttpGet httpGet =
new
HttpGet(path);
// 包装一个GET请求
try
{
HttpResponse response = client.execute(httpGet);
// 客户端执行请求
int
code = response.getStatusLine().getStatusCode();
// 获取响应码
if
(code ==
200
) {
InputStream is = response.getEntity().getContent();
// 获取实体内容
String result = StreamTools.streamToString(is);
// 字节流转字符串
return
result;
}
else
{
return
网络访问失败;
}
}
catch
(Exception e) {
e.printStackTrace();
return
网络访问失败;
}
}
/**
* 通过HttpClient发送POST请求
*
* @param username
* @param password
* @return
*/
public
static
String loginByHttpClientPOST(String username, String password) {
String path = http:
//192.168.0.107:8080/WebTest/LoginServerlet;
try
{
HttpClient client =
new
DefaultHttpClient();
// 建立一个客户端
HttpPost httpPost =
new
HttpPost(path);
// 包装POST请求
// 设置发送的实体参数
List<namevaluepair> parameters =
new
ArrayList<namevaluepair>();
parameters.add(
new
BasicNameValuePair(username, username));
parameters.add(
new
BasicNameValuePair(password, password));
httpPost.setEntity(
new
UrlEncodedFormEntity(parameters, UTF-
8
));
HttpResponse response = client.execute(httpPost);
// 执行POST请求
int
code = response.getStatusLine().getStatusCode();
if
(code ==
200
) {
InputStream is = response.getEntity().getContent();
String result = StreamTools.streamToString(is);
return
result;
}
else
{
return
网络访问失败;
}
}
catch
(Exception e) {
e.printStackTrace();
return
访问网络失败;
}
}</namevaluepair></namevaluepair>