import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map; public class HttpUtils { private static String PATH = "http://169.254.32.172:8080/myhttp/LoginAction";
private static URL url; public HttpUtils() {
// TODO Auto-generated constructor stub
} static {
try {
url = new URL(PATH);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
*
* @param params
* 填写的URL的参数
* @param encode
* 字节编码
* @return
*/
public static String sendPostMessage(Map<String, String> params,
String encode) {
// 作为StringBuffer初始化的字符串
StringBuffer buffer = new StringBuffer();
// buffer.append("?");
try {
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
buffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
}
// 删除掉最后一个&
// buffer.deleteCharAt(buffer.length()-1);
System.out.println(buffer.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setConnectTimeout(3000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);// 从服务器读数据
urlConnection.setDoOutput(true);// 向服务器写数据
// 获得上传信息的字节大小以及长度
byte[] mydata = buffer.toString().getBytes();
// 表示设置请求体的类型是文本类型
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length",
String.valueOf(mydata.length));
// 获得输出流,向服务器输出数据
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(mydata, 0, mydata.length);
outputStream.close();
// 获得服务器响应码
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
return changeInputStream(urlConnection.getInputStream(), encode);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
} /**
* 将一个输入流转换成指定编码的字符串
*
* @param inputStream
* @param encode
* @return
*/ private static String changeInputStream(InputStream inputStream,
String encode) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = "";
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
result = new String(outputStream.toByteArray(), encode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
} public static void main(String[] args) {
Map<String, String> params = new HashMap<String, String>();
params.put("username", "123");
params.put("password", "123");
sendPostMessage(params, "utf-8");
String result = sendPostMessage(params, "utf-8");
System.out.println(result);
}
}

servlet部分

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoginAction extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
this.doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String username=request.getParameter("username");
System.out.println(username);
String password=request.getParameter("password");
System.out.println(password);
if(username.equals("123")){
out.println("Success");
}else{
out.print("fail");
}
out.flush();
out.close();
} }

利用Post方法进行数据提交的更多相关文章

  1. 利用ajaxSubmit()方法实现Form提交表单后回调

    1.      背景 最近在工作中,需要实现网页端图片上传到FTP服务器的功能.上传文件是用Form表单提交数据的方法向后台传输文件流,在此遇到了一个问题:后台在处理完图片上传功能后,需要向前台回传是 ...

  2. C++使用POST方法向网页提交数据-----C++发送HTTP数据获取Google天气预报

    例1:C++使用POST方法向网页提交数据    转自:http://www.it165.net/pro/html/201208/3534.html 在C++中可以使用POST方法向网页中提交数据,这 ...

  3. 利用asynchttpclient开源项目来把数据提交给服务器

    可以通过github去查找asynchttpclient,并下载源代码,并加载到自己的工程中. 1.利用get方法提交 2.利用post方法来提交

  4. YUV420数据和字符信息如何利用滤镜方法进行编码?

    YUV420数据和字符信息如何利用滤镜方法进行编码?我希望用ffmpeg中的filter方法,把YUV420数据和字符信息一起编码,该怎么办呢? 本人目前只实现了把yuv420的数据进行h.264的编 ...

  5. 数据可视化之powerBI技巧(二十二)利用这个方法,帮你搞定Power BI"增量刷新"

    Power BI的增量刷新功能现在已经对Pro用户开通,但由于种种限制,很多人依然无法使用无这个功能,所以,每一次刷新,都要彻底更新数据集.这对于量级比较大的数据集来说,着实是一件耗费时间的事情. 拿 ...

  6. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  7. Struts2.5 利用Ajax将json数据传值到JSP

    AJAX +JSON=>JSP AJAX AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着 ...

  8. 网络相关系列之三:通过GET和POST方法发送数据

    写在最前面:年少的安逸舒适在随着年龄的到来和现实生活的压迫总有一天会全数归还(事实上就是<无间道>中那句:"出来混,迟早要还的!") so fighting. 一.GE ...

  9. 大数据学习day33----spark13-----1.两种方式管理偏移量并将偏移量写入redis 2. MySQL事务的测试 3.利用MySQL事务实现数据统计的ExactlyOnce(sql语句中出现相同key时如何进行累加(此处时出现相同的单词))4 将数据写入kafka

    1.两种方式管理偏移量并将偏移量写入redis (1)第一种:rdd的形式 一般是使用这种直连的方式,但其缺点是没法调用一些更加高级的api,如窗口操作.如果想更加精确的控制偏移量,就使用这种方式 代 ...

随机推荐

  1. 【并行计算与CUDA开发】英伟达硬件加速编解码

    硬件加速 并行计算 OpenCL OpenCL API VS SDK 英伟达硬件编解码方案 基于 OpenCL 的 API 自己写一个编解码器 使用 SDK 中的编解码接口 使用编码器对于 OpenC ...

  2. VS.2017下载安装_ZC

    ZC:20190623 1.我现在下载的是 社区版 C++的桌面开发 和 C#的开发,下载 文件压缩为:移动硬盘“H:\ZC_IDE\VC\vs2017_cs_cpp(community).rar” ...

  3. php redis mysql apache 下载地址

    Mysql:https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz php:ht ...

  4. [Cometoj#3 D]可爱的菜菜子_线段树_差分_线性基

    可爱的菜菜子 题目链接:https://cometoj.com/contest/38/problem/D?problem_id=1543 数据范围:略. 题解: 首先,如果第一个操作是单点修改,我们就 ...

  5. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

  6. Django-djangorestframework-响应模块

    响应模块 一般都用 Response 对象来做返回(最后一定是打包成符合 HTTP 协议的数据格式来传输,Response 类做了一系列处理,所以这里我们只需要关注下它的那些参数即可) 响应类构造器 ...

  7. oracle数据库的冷备份

    前言 冷备份是Oracle最简单的一种备份,所谓的冷备份指的就是在关闭数据库实例的情况下进行数据库备份操作的实现:然后使用操作系统实用工具或者第三方工具备份所有相关的数据库文件.能简单快速地备份.能简 ...

  8. 数据库(mysql和oracle)

    1.   mysql索引:    https://www.jikewenku.com/22030.html 2.

  9. 安川机器人Yaskawa

    安川机器人自动回原点 1. 判断机器人是否在安全位置  (立方体干涉区) 2. 读取机器人当前位置  GETS PX000 $PX001 读取基座标下的机器人当前位置并放到位置型变量P000中  $P ...

  10. 用于Linq的去重 Distinct

            /// <summary>         /// 用于Linq的去重,扩展方法需要放到静态类中         /// </summary>          ...