天天看點

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;

    public WeatherReport() {

        super();

    }

    @Override

    protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {

        // TODO Auto-generated method stub

        doPost(arg0, arg1);

    }

    @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%>" target="_blank" rel="external nofollow" >

    <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" target="_blank" rel="external nofollow" >

    -->

  </head>

  <body>

    <a href="<%=basePath%>/WeatherReport" target="_blank" rel="external nofollow" >weather report</a>

  </body>

</html>