http模拟登陆及发请求

 2023-09-08 阅读 15 评论 0

摘要:首先声明下,如果服务端写入的cookie属性是HttpOnly的,程序是不能自动获取cookie的,需要人工登陆网站获取cookie再把cookie写死,如下图所示: http测试工具:http://www.atool.org/httptest.php package com.eshore.ismp.hbinterfac

首先声明下,如果服务端写入的cookie属性是HttpOnly的,程序是不能自动获取cookie的,需要人工登陆网站获取cookie再把cookie写死,如下图所示:

http测试工具:http://www.atool.org/httptest.php

 

package com.eshore.ismp.hbinterface.vsop.client.productCancelBatcher;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;import com.alibaba.fastjson.JSONObject;public class HttpLoginUtils {private final static String UTF8="UTF8";private final static String MD5="MD5";private final static char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd','e', 'f' };private String cookie="JSESSIONID=E1487B3796D918D418DD8D184278C37E; LBN=node20; login_userId=24%2Cadmin_0%2C%E7%AE%A1%E7%90%86%E5%91%98; login_userType=1";private static final String userName="admin_0";private static final String password="Ismp!@#$";public static final String type="1";public static final String inputCode="JUGG";public static final String oldCode="JUGG";public static final String LOGIN_DCN_URL="http://127.0.0.1:30082/adminweb/user_login.do?";public static final String LOGIN_URL="http://127.0.0.1:9000/adminweb/user_login.do?";private static final String CANCEL_URL="http://127.0.0.1:9001/adminweb/hbmonth/productCancel.do?";public void request(String tel,long time,String contentId) throws MalformedURLException,IOException{/*try{cookie=this.login();}catch(Exception e){e.printStackTrace();}*///发送请求JSONObject productList = new JSONObject();productList.put("contentId",contentId+"@"+contentId);productList.put("startTime", time);productList.put("tel", tel);String param = "productList=["+productList+"]&source=12"+"&operatorAccount=admin_0"+"&operatorName=管理员";URL url = new URL(CANCEL_URL);HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");httpConn.setDoOutput(true);httpConn.setRequestMethod("POST");httpConn.setUseCaches(false);System.out.println("cookies:"+cookie);httpConn.setRequestProperty("Cookie",cookie);OutputStream output = httpConn.getOutputStream();output.write(param.getBytes());OutputStreamWriter outr = new OutputStreamWriter(output,"UTF-8");outr.flush();outr.close();BufferedReader redoReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));StringBuffer redoBuf = new StringBuffer();String line = null;while ((line = redoReader.readLine()) != null) {redoBuf.append(line);}System.out.println("result info:"+redoBuf.toString());}/*** @return* 获取cookie* @throws MalformedURLException,IOException */public String login() throws MalformedURLException,IOException{String cookie="";String pswd = this.MD5Encode(password);String param = "&username="+userName+"&password="+pswd+"&type="+type+"&inputCode="+inputCode+"&oldCode="+oldCode;URL url=new URL(LOGIN_URL);HttpURLConnection httpConn=(HttpURLConnection) url.openConnection();//设定传送的内容类型是可序列化的java对象 (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException) httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");//设置是否从httpUrlConnection读入,默认情况下是truehttpConn.setDoOutput(true);httpConn.setRequestMethod("POST");//Post 请求不能使用缓存httpConn.setUseCaches(false);httpConn.setInstanceFollowRedirects(false); //httpConn.connect();// httpConn.getOutputStream();隐形调用了httpConn.connect();OutputStream output = httpConn.getOutputStream();//写入输出流output.write(param.getBytes());OutputStreamWriter outr = new OutputStreamWriter(output,"UTF-8");outr.flush();outr.close();//创建读对象BufferedReader loginReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));StringBuffer sb = new StringBuffer();String line = null;//一行一行读取while ((line = loginReader.readLine()) != null) {sb.append(line);}System.out.println("output :"+sb.toString());//获取服务端返回的cookiecookie = httpConn.getHeaderField("Set-Cookie");return cookie;}public  String MD5Encode(String originalString) {try {byte[] originalBytes = originalString.getBytes(UTF8);MessageDigest md = MessageDigest.getInstance(MD5);md.update(originalBytes);byte[] temps = md.digest();int j = temps.length;char[] str = new char[j * 2];int k = 0;for (int i = 0; i < j; i++) {byte tempByte = temps[i];str[k++] = HEX_DIGITS[tempByte >>> 4 & 0xf];str[k++] = HEX_DIGITS[tempByte & 0xf];}return new String(str).toUpperCase();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}/*** @param args* test* @throws IOException * @throws MalformedURLException */public static void main(String[] args) throws MalformedURLException, IOException {String cookie=new HttpLoginUtils().login();System.out.println("cookie:"+cookie);}}

  

如何模拟并发请求? 

转载于:https://www.cnblogs.com/JAYIT/p/9680210.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/20123.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息