import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class HttpInvoker
{
public static final String GET_URL = "http://192.168.101.9/HelloWorld";
public static final String POST_URL = "http://192.168.101.9/HelloWorld";
public static void readContentFromGet() throws IOException
{
// 拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码
String getURL = GET_URL + "?name="
+ URLEncoder.encode("a fat man", "utf-8");
URL getUrl = new URL(getURL);
// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返
//回的是HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) getUrl
.openConnection();
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()
//函数中才会真正发到服务器
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "utf-8"));
System.out.println("=============================");
System.out.println("Contents of get request");
System.out.println("=============================");
String lines;
while ((lines = reader.readLine()) != null)
{
System.out.println(lines);
}
reader.close();
// 断开连接
connection.disconnect();
System.out.println("=============================");
System.out.println("Contents of get request ends");
System.out.println("=============================");
}
public static void readContentFromPost() throws IOException
{
// Post请求的url,与get不同的是不需要带参数
URL postUrl = new URL(POST_URL);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
// Output to the connection. Default is
// false, set to true because post
// method must write something to the
// connection
// 设置是否向connection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true
connection.setDoOutput(true);
// Read from the connection. Default is true.
connection.setDoInput(true);
// Set the post method. Default is GET
connection.setRequestMethod("POST");
// Post cannot use caches
// Post 请求不能使用缓存
connection.setUseCaches(false);
// This method takes effects to
// every instances of this class.
// URLConnection.setFollowRedirects
//是static函数,作用于所有的URLConnection对象。
// connection.setFollowRedirects(true);
// This methods only
// takes effacts to this
// instance.
// URLConnection.setInstanceFollowRedirec
ts是成员函数,仅作用于当前函数
connection.setInstanceFollowRedirects(true);
// Set the content type to urlencoded,
// because we will write
// some URL-encoded content to the
// connection. Settings above must be set before connect!
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode
// 进行编码
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
// The URL-encoded contend
// 正文,正文内容其实跟get的URL中'?'后的参数字符串一致
//String content = "name=" + URLEncoder.encode("中国人", "UTF-8");
String content = "name=" + URLEncoder.encode("#中国人#", "UTF-8");
String district = "&district=" + URLEncoder.encode("区域显示可", "UTF-8");
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
out.writeBytes(content);
out.writeBytes(district);
out.flush();
out.close(); // flush and close
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8"));
String line;
System.out.println("=============================");
System.out.println("Contents of post request");
System.out.println("=============================");
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
System.out.println("=============================");
System.out.println("Contents of post request ends");
System.out.println("=============================");
reader.close();
connection.disconnect();
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
try
{
readContentFromGet();
readContentFromPost();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------
package test;
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
//req.setCharacterEncoding("GBK");
//String reqEncoding = req.getCharacterEncoding();
//String name = req.getParameter("name");
//name = new String(name.getBytes("ISO-8859-1"), "GBK");
//String reqURL = req.getRequestURI();
//res.setContentType("text/html");
//res.setCharacterEncoding("UTF-8");
//PrintWriter out = res.getWriter();
//out.println("Hello World");
//String zhStr = "中文中国显示";
//out.println(zhStr);
//out.println(name);
//out.println("reqEncoding = " + reqEncoding );
//out.println("reqURL = " + reqURL );
try
{
String name = req.getParameter("name");
res.setCharacterEncoding("UTF-8");
PrintWriter out = res.getWriter();
//String jsonAd = "{ "idText":"AdMob by ToucheMedia", "idIcon":"http://192.168.101.9/icon/EXP.png", "clickURL":"http://192.168.101.9/addir/tmad.html" }";
String jsonAd = "For jsonAd test";
out.println( jsonAd );
out.println("中文中国");
out.println(name);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
try
{
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
String district = req.getParameter("district");
res.setCharacterEncoding("UTF-8");
PrintWriter out = res.getWriter();
out.println("post echo from server");
out.println("中文中国, china");
out.println("name = " + name);
out.println("district = " + district);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
分享到:
相关推荐
本篇文章主要聚焦于使用`HttpURLConnection`类来发送POST和GET请求,这是Android SDK内置的一种HTTP请求方法,相较于第三方库如OkHttp或Volley,使用`HttpURLConnection`更轻量级,易于理解和控制。 首先,我们先...
GET请求通常用于获取数据,而POST请求用于提交数据。`HttpConnUtils.jar`是一个Java工具类库,它简化了通过URL执行这两种HTTP请求的过程,并允许传递参数。下面我们将详细探讨如何使用此类库以及相关的HTTP基础知识...
下面是一个简单的GET请求示例: ```java URL url = new URL("http://example.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); ...
在Java编程中,发送HTTP POST和GET请求是常见的任务,特别是在与Web服务交互或进行自动化测试时。HTTP协议是互联网上应用最广泛的一种网络协议,它定义了客户端(如浏览器或Java应用程序)如何向服务器请求数据,...
在这个场景下,我们将详细探讨如何使用Java原生的HttpURLConnection实现一个GET请求。 首先,我们需要了解GET请求的基本原理。GET请求是最常见的HTTP方法,用于从服务器获取资源。它将参数附加到URL中,以便服务器...
Java中发送GET请求非常简单: ```java URL url = new URL("http://example.com/resource/1"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); ...
这里我们将深入探讨如何使用Java发送GET和POST请求,以及处理JSON数据。 首先,让我们关注GET请求。GET请求主要用于从服务器获取资源,其参数通常包含在URL中。在Java中,可以使用`HttpURLConnection`类或者第三方...
在Java中,我们通常使用`java.net.URL`和`java.net.HttpURLConnection`来发送GET请求。以下是一个简单的示例: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net....
`httpURLConnectionPOST()`方法展示了POST请求的处理方式,通过设置`setDoOutput(true)`开启输出流,但示例中并未写入实际的POST数据。 在实际应用中,可能需要添加更多的配置,比如设置请求头(如`Content-Type`,...
在Java编程中,HTTP POST和GET请求是网络通信的基础,广泛应用于Web服务、API调用以及数据交互。本文将深入探讨这两种请求方法及其在Java中的实现,特别关注带有回调功能的场景。 首先,GET请求是HTTP协议中最常见...
在Java编程中,HTTPURLConnection是Java标准库提供的一种用于处理HTTP连接的类,它允许我们发送HTTP请求并接收响应。然而,HTTP协议本身是无状态的,这意味着每次请求都是独立的,不会记住之前的交互,这对于需要...
JAVA通过HttpURLConnection上传和下载文件的方法 JAVA通过HttpURLConnection上传和下载文件的方法是非常有实用价值的,...同时,HttpURLConnection也可以用于发送其他类型的HTTP请求,例如GET、POST、PUT、DELETE等。
在Java中,我们可以使用`java.net.URL`和`java.net.HttpURLConnection`类来发送GET请求。首先创建URL对象,然后通过openConnection()方法获取HttpURLConnection实例,设置请求方法为GET,并通过connect()方法建立...
1. **发送GET请求**: GET请求通常用于获取资源,参数包含在URL中。以下是一个简单的示例: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; ...
本篇文章将详细介绍如何在Java中实现GET和POST请求,以及相关的知识点。 首先,我们要了解GET和POST的区别。GET请求通常用于获取资源,其参数附加在URL后面,是可见的,且对数据长度有限制,一般不超过2KB。而POST...
- GET请求常用于获取服务器上的资源,它是无状态的,参数通过URL传递。`sendGetRequest`方法会构建一个URL字符串,将所有参数附加到URL路径后,然后使用`HttpURLConnection`或者第三方库如Apache HttpClient、...
接下来,我们可以使用`HttpURLConnection`或第三方库如Apache HttpClient来发送POST请求。以下是一个使用`HttpURLConnection`的例子: ```java import java.io.OutputStream; import java.net.HttpURLConnection; ...
在Java编程中,远程接口调用是分布式系统中常见的通信方式,它允许不同的服务之间进行数据交换和功能调用。...通过这些服务,开发者可以方便地进行远程接口调用,无论是简单的GET请求还是带有复杂参数的POST请求。
1. HttpURLConnection GET请求: GET请求是最基础的HTTP请求方法,常用于获取服务器上的资源。使用HttpURLConnection进行GET请求,主要步骤包括: - 创建URL对象并打开连接:`URL url = new URL(...
// 使用HttpURLConnection发送GET请求 HttpURLConnection connection = (HttpURLConnection) new URL(fullUrl).openConnection(); // 设置请求方法为GET connection.setRequestMethod("GET"); // 获取响应码,...