怎么利用HttpURLConnection向服务器发送键值对
来源:互联网 发布:该怎么写淘宝店铺介绍 编辑:程序博客网 时间:2024/03/29 14:11
一般情况下HttpURLConnection利用Get方法可以直接通过url向服务器发送键值对。这种方式下数据往往是以url?name=value&name1=value1.....的形式向服务器传送数据的。利用OutputStream直接向服务器写入字节流即可。但是对于一些比较隐秘的数据,用户并不想通过这种显示的方式向数据库传送数据。所有这时候即使是少量的数据也要用post方式来传送。对于post传递键值对一般有两种方式,一种是直接放在http头中(也就是所谓的首部行了)向服务器传送,另一种是放在数据流中OutputStream。下面是第一种传送方式的简单介绍:
方法很简单,在客户端将键值对存储在http首部行中,然后在服务器端使用getHeader()的方法读取键值对。下面是具体代码部分:
import java.net.HttpURLConnection;import java.net.URL;public class HttpConnectonTest {public static void main(String[] agrs){String usrStr = "http://localhost:8080/LearningPlatfromServer//servlet/RegisterServlet";try {URL url = new URL(usrStr);//创建一个URLHttpURLConnection connection = (HttpURLConnection)url.openConnection();//通过该url获得与服务器的连接connection.setDoOutput(true);connection.setDoInput(true);connection.setRequestMethod("POST");//设置请求方式为postconnection.setConnectTimeout(3000);//设置超时为3秒connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置传送类型connection.setRequestProperty("user_name", "yangwan");connection.setRequestProperty("user_password", "12345");connection.setRequestProperty("user_email", "632024528@qq.con");//post方法要传送的键值对connection.connect();int responseCode = connection.getResponseCode();//获得连接的状态码if(responseCode == 200){//200表示连接服务器成功,且获得正确响应System.out.println("连接服务器成功");}else{System.out.println("连接服务器失败"+responseCode);}} catch (Exception e) {e.printStackTrace();}}}代码部分还是很好理解的。该服务器端是MyEclipse创建的一个Web Service,应用本地服务器tomcat.请求url是服务器端的一个Servlet。下面是这个Servlet中的响应客户端请求的具体代码:
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class RegisterServlet extends HttpServlet {public RegisterServlet() {super();}public void destroy() {super.destroy(); }public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request,response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String userName = request.getHeader("user_name");String userPassword = request.getHeader("user_password");String userEmail = request.getHeader("user_email");System.out.println("user_name: "+userName);System.out.println("user_password: "+userPassword);System.out.println("user_email: "+userEmail);}public void init() throws ServletException {}}这样在客户端(Eclipse)中运行HttpURLConnection的那段程序后,在服务器端(MyEclipse)中的控制台就会出现下面的结果:
1 0
- 怎么利用HttpURLConnection向服务器发送键值对
- 使用HttpURLConnection向服务器发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求
- Android使用HttpURLConnection向服务器发送post和get请求
- 利用TJSONArray向REST服务器发送文件
- 利用TJSONArray向REST服务器发送文件 .
- HttpURLConnection从客户端向服务器发送Http请求以及服务器响应全过程解析
- Java利用HttpURLConnection发送请求
- 怎么向服务器发送请求,得到返回值
- URL和HttpURLConnection的使用(向服务器发送请求,然后返回struts版)
- IT忍者神龟之使用HttpURLConnection向服务器发送post和get请求
- HttpURLConnection 向服务器发送post和get请求 并接收响应
- java基础——用HttpURLConnection向服务器发送POST请求
- HttpURLConnection往服务器发送请求
- Java服务器利用JPush 向客户端发送消息
- httpUrlConnection 实现向手机号发送验证码
- iOS中的CoreData的使用(详解)
- 学习笔记 BIT(树状数组)
- Linux下修改Mysql的用户(root)的密码
- 20151123 ocp 052(10-30)
- 为了相同的前缀-北京热身复仇
- 怎么利用HttpURLConnection向服务器发送键值对
- 为了相同的前缀-公式计算
- 解决Volley框架下JsonObjectRequest以POST提交数据,服务端无法获取数据的问题。
- 【Leetcode】刷题记之happy number
- 运行地址,链接地址,加载地址,存储地址 位置无关码、位置有关码
- 重拾 hadoop mapreduce 学习 一
- 2015年11月23日19:17:54
- 为了相同的前缀-麦迪时刻
- Java基础——Java笔记——代码块