HttpUrlConnection 獲取天氣預報

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>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章