在手机开发中,要想向服务器发送请求和读取服务器回送回来的响应,我们就需要使用到HttpUrlConnection这个类。关于这个类的详解,可以查看Java API帮助文档。
现在我们使用HttpUrlConnection这个类来模似IE,向服务器发送请求和读取服务器回送回来的响应。
- 处理服务器回送的http响应
在服务器端新建一个servlet——ServletDemo1.java,代码如下所示:
public class ServletDemo1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("hahahahaha");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
然后编写IeDemo类来使用HttpUrlConnection这个类模似IE处理服务器回送的http响应,IeDemo类的代码如下所示:
public class IeDemo {
public static void main(String[] args) throws Exception {
response();
}
public static void response() throws Exception {
URL url = new URL("http://localhost:8080/day12/ServletDemo1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println(conn.getResponseCode());
System.out.println(conn.getHeaderField("Content-Length"));
InputStream in = conn.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len=in.read(buffer))>0) {
System.out.println(new String(buffer,0,len));
}
}
}
运行结果如下:
- 向服务器发送http请求
在服务器端新建一个servlet——ServletDemo1.java,代码如下所示:
public class ServletDemo1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String header = request.getHeader("referer");
System.out.println(header);
String name = request.getParameter("name");
System.out.println(name);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
现在我们就要编写IeDemo类来使用HttpUrlConnection这个类模似IE向服务器发送http请求。第一次编写IeDemo类,代码如下:
public class IeDemo {
public static void main(String[] args) throws Exception {
request();
}
public static void request() throws Exception {
URL url = new URL("http://localhost:8080/day12/ServletDemo1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("referer", "http://www.sina.com");
OutputStream out = conn.getOutputStream();
out.write("name=aaaa".getBytes());
}
}
此时运行会发现抛异常如下:
Exception in thread "main" java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
这说明HttpURLConnection对象默认是无法向服务器写数据的。所以应该修改IeDemo类的代码为如下:
public class IeDemo {
public static void main(String[] args) throws Exception {
request();
}
public static void request() throws Exception {
URL url = new URL("http://localhost:8080/day12/ServletDemo1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("referer", "http://www.sina.com");
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write("name=aaaa".getBytes());
}
}
这样修改好之后是不是万事大吉了呢?不是,运行以上程序,服务器在控制台并没有任何输出。这又是什么原因引起来的呢?
答:一个完整的http请求是由请求和响应两部分组成的,以上程序只有向服务器发送的请求,而没有服务器回送回来的响应,所以这不是一个完整的http请求,才导致这样的问题发生。
对于这样的问题,那又该怎么解决呢?
答:在程序中应该处理http响应,例如得到http响应状态码。代码应修改为如下:
public class IeDemo {
public static void main(String[] args) throws Exception {
request();
}
public static void request() throws Exception {
URL url = new URL("http://localhost:8080/day12/ServletDemo1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("referer", "http://www.sina.com");
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write("name=aaaa".getBytes());
System.out.println(conn.getResponseCode());
}
}
运行该程序服务器控制台输出如下:
IeDemo类代码还可以修改为如下:
public class IeDemo {
public static void main(String[] args) throws Exception {
request();
}
public static void request() throws Exception {
URL url = new URL("http://localhost:8080/day12/ServletDemo1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == 200) {
conn.setRequestMethod("POST");
conn.setRequestProperty("referer", "http://www.sina.com");
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write("name=aaaa".getBytes());
}
}
}