`

HttpUrlConnection模拟浏览器发送请求

 
阅读更多

servlet...code

package cn.itcast;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OneServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
String name = request.getParameter("name");
System.err.println("这是get、、、、"+name);
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().print("你好:"+name);
}

public void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
System.err.println("这是post请求......."+name);
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().print("你好:"+name);
}

}


测试类

package cn.demo;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.Test;
public class Demo {
/**
* 发送get请求
* @throws Exception
*/
@Test
public void testConn() throws Exception{
//第一步:声明url
String urlPath = "http://localhost:6666/day22_cos/OneServlet?name=Jack";
//第二步:声明URL对象
URL url = new URL(urlPath);
//第三步:从url上获取连接
HttpURLConnection con=  (HttpURLConnection) url.openConnection();
//第四步:设置访问的类型
con.setRequestMethod("GET");
//第五步:设置可以向服务器发信息。也可以从服务器接收信息
con.setDoInput(true);//设置可以向服务器发信息
con.setDoOutput(true);//也可以从服务器接收信息
//第六步:连接
con.connect();
//7:检查连接状态
int code = con.getResponseCode();
if(code==200){
//8:从服务器读取数据
InputStream in = con.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=in.read(b))!=-1){
String s = new String(b,0,len,"UTF-8");
System.err.print(s);
}
}
//9:断开
con.disconnect();
}
/**
* 以下发送post请求
*/
@Test
public void post() throws Exception{
//第一步:声明url
String urlPath = "http://localhost:6666/day22_cos/OneServlet";
//第二步:声明URL对象
URL url = new URL(urlPath);
//第三步:从url上获取连接
HttpURLConnection con=  (HttpURLConnection) url.openConnection();
//第四步:设置访问的类型
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//第五步:设置可以向服务器发信息。也可以从服务器接收信息
con.setDoInput(true);//设置可以向服务器发信息
con.setDoOutput(true);//也可以从服务器接收信息
//第六步:发信息
//获取输出流
OutputStream out = con.getOutputStream();
out.write("name=张三".getBytes("UTF-8"));


//7:检查连接状态
int code = con.getResponseCode();
if(code==200){
//8:从服务器读取数据
InputStream in = con.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=in.read(b))!=-1){
String s = new String(b,0,len,"UTF-8");
System.err.print(s);
}
}
//9:断开
con.disconnect();
}
}




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics