统一的URL编码
package demo0808.demo2; /** * 由于网络上HTTP的POST请求需要进行编码 * 以此来适应不同的操作系统 * 编码成统一的ASCII字符 * 本类对Query语句进行URL编码 * 键值对中间用=连接 * 不同的键值对用&连接 */ import java.net.URLEncoder; public class QueryString { private StringBuilder query=new StringBuilder(); private String charset="UTF-8"; /** * 无参默认构造函数 */ public QueryString() { } /** * 同步添加键值对 * @param name 参数名称 * @param value 参数值 */ public synchronized void add(String name,String value) { query.append('&'); encode(name,value); } /** * 同步进行URL编码 * @param name 参数名称 * @param value 参数值 */ private synchronized void encode(String name,String value) { try { query.append(URLEncoder.encode(name, charset)); query.append('='); query.append(URLEncoder.encode(value,charset)); } catch (Exception ex) { System.err.println("Bad VM can't support "+charset); } } /** * 同步获取查询字符串 * @return 字符串 */ public synchronized String getQuery() { return query.toString(); } /** * 覆盖toString()方法 */ public String toString() { return getQuery(); } }
基于Http的post请求键值对填写
package demo0808.demo2; /** * 针对HTTP协议的POST请求 * 表单自动填写 * 不考虑验证码识别 * 不考虑https类型的协议 */ import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; public class FormPoster { private URL url; private QueryString queryString=new QueryString() ; /** * 构造函数 * @param url 统一资源位置符号 */ public FormPoster(URL url) { if(!url.getProtocol().toLowerCase().startsWith("http")) { System.err.println("Post只用于http协议!"); return; } this.url=url; } /** * 添加参数-值对 * @param name 参数名称 * @param value 参数值 */ public void add(String name,String value) { queryString.add(name, value); } /** * 获取URL * @return URL */ public URL getURL() { return url; } /** * 获取网站相应的输入流 * @return 输入流 * @throws Exception 无法连接异常 */ public InputStream post() throws Exception { URLConnection openConnection = url.openConnection(); openConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); openConnection.setDoOutput(true); OutputStream outputStream = openConnection.getOutputStream(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8"); outputStreamWriter.write(queryString.toString()); outputStreamWriter.write("/r/n"); outputStreamWriter.flush(); outputStreamWriter.close(); //刷新关闭流很重要,只有关闭了,才能获取输入流 return openConnection.getInputStream(); } }
测试代码
package demo0808.demo2; /** * 测试代码 */ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; public class Test { public static void main(String[] args) throws Exception { URL url = new URL("https://uniportal.huawei.com/uniportal/login.do"); FormPoster formPoster = new FormPoster(url); /** * 以下是测试华为招聘网站登陆界面 */ formPoster.add("actionFlag", "loginAuthenticate"); formPoster.add("lang", "zh"); formPoster.add("redirect", "http://career.huawei.com/recruitment/login_index.html?redirect=http://career.huawei.com/recruitment/"); formPoster.add("uid", "用户名"); formPoster.add("password", "密码`"); InputStream inputStream = formPoster.post(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); File file=new File("outfile"); FileOutputStream fileOutputStream = new FileOutputStream(file); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8"); int c; while((c=inputStreamReader.read())!=-1) { System.out.print((char)c); outputStreamWriter.write((char)c); } inputStream.close(); outputStreamWriter.close(); System.out.println(); } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10571.html