利用Post方法进行数据提交
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方法进行数据提交的更多相关文章
- 利用ajaxSubmit()方法实现Form提交表单后回调
1. 背景 最近在工作中,需要实现网页端图片上传到FTP服务器的功能.上传文件是用Form表单提交数据的方法向后台传输文件流,在此遇到了一个问题:后台在处理完图片上传功能后,需要向前台回传是 ...
- C++使用POST方法向网页提交数据-----C++发送HTTP数据获取Google天气预报
例1:C++使用POST方法向网页提交数据 转自:http://www.it165.net/pro/html/201208/3534.html 在C++中可以使用POST方法向网页中提交数据,这 ...
- 利用asynchttpclient开源项目来把数据提交给服务器
可以通过github去查找asynchttpclient,并下载源代码,并加载到自己的工程中. 1.利用get方法提交 2.利用post方法来提交
- YUV420数据和字符信息如何利用滤镜方法进行编码?
YUV420数据和字符信息如何利用滤镜方法进行编码?我希望用ffmpeg中的filter方法,把YUV420数据和字符信息一起编码,该怎么办呢? 本人目前只实现了把yuv420的数据进行h.264的编 ...
- 数据可视化之powerBI技巧(二十二)利用这个方法,帮你搞定Power BI"增量刷新"
Power BI的增量刷新功能现在已经对Pro用户开通,但由于种种限制,很多人依然无法使用无这个功能,所以,每一次刷新,都要彻底更新数据集.这对于量级比较大的数据集来说,着实是一件耗费时间的事情. 拿 ...
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- Struts2.5 利用Ajax将json数据传值到JSP
AJAX +JSON=>JSP AJAX AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着 ...
- 网络相关系列之三:通过GET和POST方法发送数据
写在最前面:年少的安逸舒适在随着年龄的到来和现实生活的压迫总有一天会全数归还(事实上就是<无间道>中那句:"出来混,迟早要还的!") so fighting. 一.GE ...
- 大数据学习day33----spark13-----1.两种方式管理偏移量并将偏移量写入redis 2. MySQL事务的测试 3.利用MySQL事务实现数据统计的ExactlyOnce(sql语句中出现相同key时如何进行累加(此处时出现相同的单词))4 将数据写入kafka
1.两种方式管理偏移量并将偏移量写入redis (1)第一种:rdd的形式 一般是使用这种直连的方式,但其缺点是没法调用一些更加高级的api,如窗口操作.如果想更加精确的控制偏移量,就使用这种方式 代 ...
随机推荐
- linux中alarm函数和pause函数详解实例
alarm(time);执行之后告诉内核,让内核在time秒时间之后向该进程发送一个定时信号,然后该进程捕获该信号并处理:pause()函数使该进程暂停让出CPU,但是该函数的暂停和前面的那个slee ...
- Oracle导出包含clob字段的sql脚本工具
之前工作中遇到生产环境不允许导入Oracle的dmp文件,只能导入sql脚本,但是表中存在clob字段,直接用plsql工具无法导出clob字段,用了下dbvisualizer可以直接导出,亲测可用. ...
- java -io 读取文件操作
主要分为字节读取和字符读取,字节读取可以一个一个读取和字节数组读取,字符读取同样之,字符读取适合文本读取,字节读取皆可以 这里直接上代码,读取文件的9个小demo package com.io; im ...
- Object类的equals方法 常用API
boolean equals (Object obj)(return this==obj): p1.equals(p2): 基本数据类型比较的是值 引用数据类型:默认比较的是两个对象的地址 ...
- javascript学习笔记 BOM和DOM详解
js组成 我们都知道, javascript 有三部分构成,ECMAScript,DOM和BOM,根据宿主(浏览器)的不同,具体的表现形式也不尽相同,ie和其他的浏览器风格迥异. 1. DOM 是 W ...
- AndroidStudio布局编辑器强制刷新布局界面
用AndroidStudio布局编辑器编辑界面的时候,在selector里调整按钮的颜色,调整后的颜色经常无法实时显示在布局编辑器里,每次都重新运行程序查看界面又非常麻烦和低效,可以用以下方法解决: ...
- java知识随笔整理-数据库的临时表
1.创建临时表的方法 方法一.select * into #临时表名 from 你的表; 方法二. create table #临时表名(字段1 约束条件,字段2 约束条件,.....)create ...
- Java web server 基本实现原理
public class WebServer { //服务端Socket只要一个,所以定义成static, 同一时间只能一个线程访问(主线程) private static ServerSocket ...
- 1.3Security:权限管理,过滤、监听、拦截
Security:权限管理 常用权限拦截器 SecurityContextPersistenceFilter 以前是HttpSesstionContextIntegrationFilter,位于过滤器 ...
- ACM-ICPC 2018 徐州赛区网络预赛 I. query 树状数组
I. query 题目链接: Problem Description Given a permutation \(p\) of length \(n\), you are asked to answe ...