1.超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。 2. java接口 --------java.net.*
3. apache 接口---------org.apache.http.*
Apache提供的HttpCient,实现起来简单方便:
A: GET方式操作
-
public void get() {
-
String url = httpUrl + "?text1=" + text1.getText().toString()
-
+ "&text2=" + text2.getText().toString();
-
// 创建HttpGet对象
-
HttpGet request = new HttpGet(url);
-
// 创建HttpClient对象
-
HttpClient client = new DefaultHttpClient();
-
HttpResponse httpResponse = null;
-
try {
-
httpResponse = client.execute(request);
-
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-
text3.setText(EntityUtils.toString(httpResponse.getEntity(),
-
"utf-8"));
-
}
-
} catch (ClientProtocolException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
public void get() {
-
String url = httpUrl + "?text1=" + text1.getText().toString()
-
+ "&text2=" + text2.getText().toString();
-
// 创建HttpGet对象
-
HttpGet request = new HttpGet(url);
-
// 创建HttpClient对象
-
HttpClient client = new DefaultHttpClient();
-
HttpResponse httpResponse = null;
-
try {
-
httpResponse = client.execute(request);
-
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-
text3.setText(EntityUtils.toString(httpResponse.getEntity(),
-
"utf-8"));
-
}
-
} catch (ClientProtocolException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
- }
B:POST方式操作
-
public void post() {
-
-
// 创建HttpPost对象
-
-
HttpPost httpRequest = new HttpPost(httpUrl);
-
-
// 创建传递参数集合
-
-
List params = new ArrayList();
-
-
params.add(new BasicNameValuePair("text1", text1.getText().toString()));
-
-
params.add(new BasicNameValuePair("text2", text2.getText().toString()));
-
-
// 设置字符集
-
-
try {
-
-
HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
-
-
httpRequest.setEntity(entity);
-
-
// 创建连接对象
-
-
HttpClient client = new DefaultHttpClient();
-
-
// 执行连接
-
-
HttpResponse response = client.execute(httpRequest);
-
-
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-
-
text3.setText(EntityUtils.toString(response.getEntity(),
-
-
"utf-8"));
-
-
}
-
-
} catch (UnsupportedEncodingException e) {
-
-
// TODO Auto-generated catch block
-
-
e.printStackTrace();
-
-
} catch (ClientProtocolException e) {
-
-
// TODO Auto-generated catch block
-
-
e.printStackTrace();
-
-
} catch (IOException e) {
-
-
// TODO Auto-generated catch block
-
-
e.printStackTrace();
-
-
}
-
-
}
-
-
public void post() {
-
-
// 创建HttpPost对象
-
-
HttpPost httpRequest = new HttpPost(httpUrl);
-
-
// 创建传递参数集合
-
-
List params = new ArrayList();
-
-
params.add(new BasicNameValuePair("text1", text1.getText().toString()));
-
-
params.add(new BasicNameValuePair("text2", text2.getText().toString()));
-
-
// 设置字符集
-
-
try {
-
-
HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
-
-
httpRequest.setEntity(entity);
-
-
// 创建连接对象
-
-
HttpClient client = new DefaultHttpClient();
-
// 执行连接
-
HttpResponse response = client.execute(httpRequest);
-
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-
text3.setText(EntityUtils.toString(response.getEntity(),
-
"utf-8"));
-
}
-
} catch (UnsupportedEncodingException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (ClientProtocolException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}