HttpUrlConnection:sun公司封装的网络连接
doGet
doPostHttpClient:Apache使用HttpURLConnection封装的类
HttpClientdoGet
HttpClientdoPost
常用编码转换
// 设置接受的数据类型
httpConnection.setRequestProperty("Accept-Charset", "utf-8");
// 设置可以接受序列化的java对象
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
deGet方法测试
JButton btnNewButton = new JButton("doGet方法测试");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String urlString="http://localhost:8080/MyServiceTest/MyTestServerlet?username=zhangsan&password=123456";
try {
URL url=new URL(urlString);//生成URL
URLConnection connect=url.openConnection();//打开url链接
//强制转型成httpurlconnection
HttpURLConnection httpConnection=(HttpURLConnection)connect;
//设置请求方法
httpConnection.setRequestMethod("GET");
//设置编码格式
// 设置接受的数据类型
httpConnection.setRequestProperty("Accept-Charset", "utf-8");
// 设置可以接受序列化的java对象
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
int code=httpConnection.getResponseCode();
System.out.println("状态码:"+code);
if(code==HttpURLConnection.HTTP_OK){
InputStream is=httpConnection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
while (line!=null){
System.out.println(line);
line=br.readLine();
}
}
#
1生成URL ——->URL url=new URL(urlString);
2开url链接——–>URLConnection connect=url.openConnection();
3强制转型httpurlconnection——->HttpURLConnection httpConnection=(HttpURLConnection)connect;
DoPost
//设置请求方法
httpConnection.setRequestMethod(“POST”);
//设置可以读取服务器返回的内容默认true
httpConnection.setDoInput(true);
//设置哭护短可以给服务器提交数据,默认为false但POST必须设置true
httpConnection.setDoOutput(true);
//POST方法不允许使用缓存
httpConnection.setUseCaches(false);
String params=”username=zhangsan&password=lisi”;
httpConnection.getOutputStream().write(params.getBytes());
public void actionPerformed(ActionEvent arg0) {
String urlString="http://localhost:8080/MyServiceTest/MyTestServerlet?username=zhangsan&password=123456";
try {
URL url=new URL(urlString);//生成URL
URLConnection connect=url.openConnection();//打开url链接
//强制转型成httpurlconnection
HttpURLConnection httpConnection=(HttpURLConnection)connect;
//设置读取时间超时
httpConnection.setReadTimeout(30000);
//设置链接超时时间
httpConnection.setConnectTimeout(30000);
//设置编码格式
// 设置接受的数据类型
httpConnection.setRequestProperty("Accept-Charset", "utf-8");
// 设置可以接受序列化的java对象
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置请求方法
httpConnection.setRequestMethod("POST");
//设置可以读取服务器返回的内容默认true
httpConnection.setDoInput(true);
//设置哭护短可以给服务器提交数据,默认为false但POST必须设置true
httpConnection.setDoOutput(true);
//POST方法不允许使用缓存
httpConnection.setUseCaches(false);
String params="username=zhangsan&password=lisi";
httpConnection.getOutputStream().write(params.getBytes());
int code=httpConnection.getResponseCode();
System.out.println("状态码:"+code);
if(code==HttpURLConnection.HTTP_OK){
InputStream is=httpConnection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
while (line!=null){
System.out.println(line);
line=br.readLine();
}
}2
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ConnectException e) {
System.out.println("服务器拒绝链接");
e.printStackTrace();
}catch ( SocketTimeoutException e) {
System.out.println("网络连接超时");
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpClientDoGet
JButton btnNewButton = new JButton("HttpClientDoGet");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String urlString="http://localhost:8080/MyServiceTest/MyTestServerlet?username=zhangsan&password=123456";
HttpClientBuilder builder=HttpClientBuilder.create();
builder.setConnectionTimeToLive(3000, TimeUnit.MILLISECONDS);
//生成client的buidler
HttpClient client=builder.build();//生成client
HttpGet get=new HttpGet(urlString);//设置为get方法
get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//设置服务器接收后数据的读取方式为utf8
try {
HttpResponse response=client.execute(get);//执行get方法得到服务器的返回的所有数据都在response中
StatusLine statusLine=response.getStatusLine();//httpClient访问服务器返回的表头,包含http状态码
int code=statusLine.getStatusCode();//得到状态码
if(code==HttpURLConnection.HTTP_OK){
HttpEntity entity=response.getEntity();//得到数据的实体
InputStream is=entity.getContent();//得到输入流
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
while(line!=null){
System.out.println(line);
line=br.readLine();
}
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
HttpClientDoPOST
public void actionPerformed(ActionEvent arg0) {
String urlString = "http://localhost:8080/MyServiceTest/MyTestServerlet";
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionTimeToLive(3000, TimeUnit.MICROSECONDS);
// 生成client的builder
HttpClient client = builder.build();
HttpPost post = new HttpPost(urlString);// 设置为get方法
NameValuePair pair1=new BasicNameValuePair("username","张三");
NameValuePair pair2=new BasicNameValuePair("password","123456");
ArrayList<NameValuePair>params=new ArrayList<>();
params.add(pair1);
params.add(pair2);
try {
post.setEntity(new UrlEncodedFormEntity(params ,"UTF-8"));
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
HttpResponse response =client.execute(post);
int code =response.getStatusLine().getStatusCode();
if(code==200){
HttpEntity entity=response.getEntity();
InputStream is=entity.getContent();
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String line =br.readLine();
while(line!=null){
System.out.println(line);
line=br.readLine();
}
}