您的位置:首页 > 理论基础 > 计算机网络

HttpUrlConnection 获取天气预报

2008-11-15 11:11 260 查看
package com.scnu.weather;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

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

public class WeatherReport extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* Constructor of the object.
*/
public WeatherReport() {
super();
}

/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(arg0, arg1);
}

/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet WeatherReport</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet WeatherReport at " + request.getContextPath() + "</h1>");
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL("http://www.gd12121.com/special/Fcst/Gzfc.htm");

httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("GET");
int code = httpurlconnection.getResponseCode();
if (code != HttpURLConnection.HTTP_OK) {
throw new Exception("code exception ");
}
InputStream is = httpurlconnection.getInputStream();
byte[] b = new byte[1024];
int i = 0;
System.out.println("reading");
do {
i = is.read(b);
if (i > 0) {
out.println(new String(b));
}
} while (i > 0);
if (is != null) {
is.close();
}
System.out.println("code " + code);

out.println("</body>");
out.println("</html>");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null) {
httpurlconnection.disconnect();
}
}
} finally {
out.close();
}
}

}

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="java.net.HttpURLConnection, java.net.URL, java.io.InputStream" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'MyJsp.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<a href="<%=basePath%>/WeatherReport">weather report</a>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: