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("");    }}

打印的内容为:

<html><head><title>SampleServlettitle>head>bodyPost Request Success!>ID:javakingname:kingbody>html>

用HttpURLConnection实现文件上传下载,,

http://blog.csdn.net/springsky_/article/details/7095034

http://www.apkbus.com/android-27613-1-1.html?_dsign=807c48ff

http://www.tuicool.com/articles/AnaaMbA

http://javaking75.blog.me/220552685641

httpclient方式

更多相关文章

  1. Android(安卓)监听wifi广播的两种方式
  2. ch07 Android(安卓)日期与时间对话框
  3. Android关于桌面快捷方式工具类!
  4. ch07 Android(安卓)日期与时间对话框
  5. Android(安卓)代码片段---获取手机通讯录列表
  6. Android(安卓)TimeLine 时间节点轴的实现
  7. Android菜单实现两种方式
  8. Android时间工具类 本地转UTC,UTC转本地
  9. 我今天的面试题,注册广播有几种方式,这些方式有何优缺点?请谈谈Andr

随机推荐

  1. android写一个Rxjava
  2. Chapter5-Android(安卓)Scroll 分析
  3. android线程、UI、AsyncTask
  4. 让Android飞!Google开始测试ART
  5. Android应用开发揭秘第3章笔记
  6. Android(安卓)SDK Document 注解【1】
  7. pc 通过vnc控制android
  8. 从零开始--系统深入学习android(实践-让我
  9. Android静默安装
  10. Android(安卓)任务栈简介