java模拟post提交
package javapost; import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest; public class RZBService { public static void main(String[] args) {
//注意:参数必须要经过utf-8编码
simpleCheck(); } static void simpleCheck(){
String account = "xxx"; // 账号
String pwd = "xxx"; // 密码
String sign = md5(md5(xxxx+ xxxx) + xxxx);
String url="xxxxxx";
String postdata="";
try {
postdata = "idNumber=" + idNumber
+ "&name=" + URLEncoder.encode(name, "UTF-8")
+ "&account=" + account
//+ "&pwd="+ pwd
+ "&sign=" + sign;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String json="";
try {
json = readContentFromPost(url, postdata);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(json);
} static String md5(String text) {
byte[] bts;
try {
bts = text.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bts_hash = md.digest(bts);
StringBuffer buf = new StringBuffer();
for (byte b : bts_hash) {
buf.append(String.format("%02X", b & 0xff));
}
return buf.toString();
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
return "";
} catch (java.security.NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
} public static String readContentFromPost(String POST_URL,String postdata) throws IOException{
URL postUrl = new URL(POST_URL);
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(postdata);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码
String line="";
StringBuffer response = new StringBuffer();
while ((line = reader.readLine()) != null){
response.append(line);
}
reader.close();
connection.disconnect();
return response.toString();
}
}
java模拟post提交的更多相关文章
- Java模拟post-get提交
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- Java模拟post提交表单数据
package test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOExcep ...
- Java中使用多线程、curl及代理IP模拟post提交和get访问
Java中使用多线程.curl及代理IP模拟post提交和get访问 菜鸟,多线程好玩就写着玩,大神可以路过指教,小弟在这受教,谢谢! 更多分享请关注微信公众号:lvxing1788 ~~~~~~ 分 ...
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...
- Java中使用多线程、curl及代理IP模拟post提交和get訪问
Java中使用多线程.curl及代理IP模拟post提交和get訪问 菜鸟,多线程好玩就写着玩.大神能够路过不吝赐教.小弟在这受教.谢谢! 很多其它分享请关注微信公众号:lvxing1788 ~~~~ ...
- Java模拟HttpClient进行Get和Post提交
使用Java模拟客户端进行提交,需要用到apache http client jar,这里用的是4.4版本 GET: public void GetURL(){ String strResp=&qu ...
- Java模拟登陆02【转载】
在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢? 方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时 ...
- java模拟form上传数据
Java模拟form表单上传 查看form表单提交的http请求为 import java.io.*; import java.net.*; public class FileUpload { /** ...
- java模拟Cookies登陆
在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢? 方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时时将co ...
随机推荐
- Python——内部参数对外部实参的影响
无论函数传递的参数的可变还是不可变,只要针对参数使用赋值语句,会在函数内部修改局部变量的引用,不会影响到外部变量的引用,而如果传递的参数是可变类型,在函数内部使用方法修改了数据的内容,同样会影响到外部 ...
- JS获取后台返回的JSON数据
问题:通过$.get从后台获取了一段json串{"id":"1","name":"ww"},然后要拿到这里面的id和na ...
- selenium 之Ran 0 tests in 0.000s
from selenium import webdriverfrom time import ctime,sleepimport unittestclass TestLogin(unittest.Te ...
- vue-webpack项目中调试的问题
在使用devtools的过程中,可以使用debugger.
- H5页面JS调试
页面调试 常用的调试方法 开发时候的调试基本是在chrome的控制台Emulation完成 现有的一些手机端调试方案: Remote debugging with Opera Dragonfly 需要 ...
- WPF与Winform中的不同(1)
1. 部分控件的Text属性,变成了 Content属性 如: winform中,Button.Text = "abc"; wpf中,Button.Content = " ...
- 【c++】iostreeam中的类为何不可以直接定义一个无参对象呢
缘起 #include <iostream> #include <fstream> #include <sstream> using namespace std; ...
- 解决IntelliJ IDEA导入本地项目不能切换github容器的问题
问题场景: 复制本地一个github项目到IDEA中,本地项目容器是A,新项目的容器仍然是A. 解决方法: 1.打开项目在资源管理器中的位置,隐藏的文件项目查看√上,删掉.git文件夹. 2. 3.选 ...
- springboot定时任务,去掉指定日期
今天用springboot写到一个需求:每周定时发送任务,但是要避开法定节假日. 网上找了些博客看,主要参考了https://www.cnblogs.com/lic309/p/4089633.html ...
- jvm options
http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#Options Categories of J ...