Android:HttpURLConnection使用,Tomcat的Servlet
16lz
2021-01-23
GET 方式:
import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.List;import java.util.Map;public class URLGetSample { public static void main(String[] args) throws IOException { URL url = new URL("http://javaking75.blog.me/rss"); System.out.println("URL :" + url.toExternalForm()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); // 链接timeout时间 conn.setConnectTimeout(3000); // 3秒 // 读timeout时间 conn.setReadTimeout(3000); // 3秒 System.out.println("getRequestMethod():" + conn.getRequestMethod()); System.out.println("getContentType():" + conn.getContentType()); System.out.println("getResponseCode():" + conn.getResponseCode()); System.out.println("getResponseMessage():" + conn.getResponseMessage()); // 打印header信息 for (Map.Entry> header : conn.getHeaderFields().entrySet()) { for (String value : header.getValue()) { System.out.println(header.getKey() + " : " + value); } } try (InputStream in = conn.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream()) { byte[] buf = new byte[1024 * 8]; int length = 0; while ((length = in.read(buf)) != -1) { out.write(buf, 0, length); } System.out.println(new String(out.toByteArray(), "UTF-8")); } conn.disconnect(); }}
运行程序,打印的内容,,
POST方法
本地的java程序:
import java.net.*;import java.util.*;import java.io.*;public class HttpURLConnection_test { public static void main(String[] args) throws IOException { URL url = new URL("http://localhost:8080/web_test/SampleServlet"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); try (OutputStream out = conn.getOutputStream()) { out.write("id=javaking".getBytes()); out.write("&".getBytes()); out.write(("name=" + URLEncoder.encode("king","UTF-8")).getBytes()); } try (InputStream in = conn.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream()) { byte[] buf = new byte[1024 * 8]; int length = 0; while ((length = in.read(buf)) != -1) { out.write(buf, 0, length); } System.out.println(new String(out.toByteArray(), "UTF-8")); } conn.disconnect(); }}
服务器上Servlet程序。编译完之后,放到Tomcat目录下。
WEB-INF\classes\com\test\SampleServlet.class
然后重新启动Tomcat。
package com.test;import java.io.IOException;import java.io.PrintWriter;import java.util.Enumeration;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class SampleServlet */@WebServlet("/SampleServlet")public class SampleServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SampleServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); Enumeration parameterNames = request.getParameterNames(); while(parameterNames.hasMoreElements()) { String name = (String) parameterNames.nextElement(); System.out.println(name + "=" + request.getParameter(name)); } response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("SampleServlet "); out.println(""); out.println("body"); out.println("Post Request Success!>"); out.println("ID:"+request.getParameter("id")); out.println("name:"+request.getParameter("name")); out.println(""); out.println("