1.使用HttpURLConnection访问网络资源
<span style="font-size:14px;">private String httpUrlForResult(URL url) {
String resultStr = "";
HttpURLConnection httpConnection = null;
InputStream in = null;
try {
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(1000);// 设置超时时间
httpConnection.setReadTimeout(10000);
// 设置请求方式,表示设置为get
// httpConnection.setRequestMethod("GET");
// 设置为post方式
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
httpConnection.setUseCaches(false);// 忽略缓存
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded"); // 输出键值对
httpConnection.setRequestProperty("Connection", "Keep-Alive"); // 维持长连接
httpConnection.setRequestProperty("Charset", "utf-8");
httpConnection
.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13");
httpConnection.setRequestProperty("Accept", "*/*");
httpConnection.setRequestProperty("Accept-Encoding", "identity");
OutputStream out = httpConnection.getOutputStream();
out.write(1);// post请求的数据,即写出的数据,这儿可根据自己的需求改
out.flush();
out.close();
in = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
int responseContent = httpConnection.getContentLength();
StringBuilder sb = new StringBuilder();
if (responseContent != 0) {
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
}
resultStr = sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return resultStr;
}</span>
2.HttpClient访问网络资源
(1)get请求
private String connServerForResult(String url) {
String strResult = "";
BufferedReader reader = null;
HttpGet httprequest = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
try {
HttpResponse httpresponse = httpclient.execute(httprequest);
reader = new BufferedReader(new InputStreamReader(httpresponse
.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
strResult = sb.toString();
return strResult;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strResult;
}
(2)post请求(只做了部分判断)
public String connServerForResultP(String url) {
String result = null;
BufferedReader reader = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("name", "zhangyabin"));
postParameters.add(new BasicNameValuePair("password", "99999999"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
reader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer strBuffer = new StringBuffer("");
String line = null;
while ((line = reader.readLine()) != null) {
strBuffer.append(line);
}
result = strBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
get和post请求的区别(资料查询所得)
1.get是从服务器上获取数据,post是向服务器传送数据。
2.get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,
将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。如果不指定Method,则默认 为GET请求,Form中提交的数据将会附加在url之后,以?分开与url分开。
3.HTTP协议没有对传输的数据大小进行限制,HTTP协议规范也没有对URL长度进行限制,开发中存在的限制主要 是:GET:特定浏览器和服务器对URL长度有限
制,其限制取决于操作系 统的支持。POST:由于不是通过URL传值,理论上数据不受限。但实际各个WEB服务器会规定对post提交数据大小进行限制,
Apache、IIS6 都有各自的配置。