备用
package http; import java.io.UnsupportedEncodingException; import sun.misc.*; public class Base64 { // ???? public String getBase64(String str) { byte[] b = null; String s = null; try { b = str.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (b != null) { s = new BASE64Encoder().encode(b); } return s; } // ???? public String getFromBase64(String s) { byte[] b = null; String result = null; if (s != null) { BASE64Decoder decoder = new BASE64Decoder(); try { b = decoder.decodeBuffer(s); result = new String(b, "utf-8"); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } return result; } }
Base64.java
package http; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class get { public static void main(String[] args) throws Exception { System.out.println(doGet()); } public static String doGet() throws Exception { URL localURL = new URL("http://www.cnblogs.com/tester-hehehe/"); URLConnection connection = localURL.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection)connection; httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader reader = null; StringBuffer resultBuffer = new StringBuffer(); String tempLine = null; if (httpURLConnection.getResponseCode() >= 300) { throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } try { inputStream = httpURLConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); reader = new BufferedReader(inputStreamReader); while ((tempLine = reader.readLine()) != null) { resultBuffer.append(tempLine); } } finally { if (reader != null) { reader.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (inputStream != null) { inputStream.close(); } } return resultBuffer.toString(); } }
get.java
package http; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class login { public JSONObject Res(){ String s="123"; String mi; mi = md5.getKeyedDigest(s, ""); // String res = "{‘md‘:‘###_pt_doc_cnt‘,‘page‘:‘hehe‘}"; String res = "{‘page‘:‘gainview‘,‘md‘:‘partner_gain_pt_doc_cnt‘}"; String result = new Base64().getBase64(res); System.out.println(result); ArrayList re = new ArrayList(); Map req1 = new HashMap(); req1.put("pid", "****"); req1.put("authId", "***"); req1.put("time", "***"); req1.put("param", result); String req = "pid"+"authID"+"time"+result+"**私钥***"; String req2; sha256 bb = new sha256(); req2 = bb.Encrypt(req, "SHA-256"); req1.put("sign", req2); System.out.println(req1); JSONObject jsonObject = JSONObject.fromObject(req1); //System.out.println(rr); return jsonObject; } public static void main(String[] argv){ login a = new login(); JSONObject b = a.Res(); System.out.println(b); } }
login.java
内容为从网上转载,直接套用 package http; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * @author King_wangyao */ public class md5 { public static byte[] getKeyedDigest(byte[] buffer, byte[] key) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(buffer); return md5.digest(key); } catch (NoSuchAlgorithmException e) { } return null; } public static String getKeyedDigest(String strSrc, String key) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(strSrc.getBytes("UTF8")); String result=""; byte[] temp; temp=md5.digest(key.getBytes("UTF8")); for (int i=0; i<temp.length; i++){ result+=Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6); } return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); } return null; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String mi; String s = "[email protected]"; //第二个参数请填空字符串 mi=md5.getKeyedDigest(s,""); System.out.println("mi:"+mi); } }
md5.java
package http; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import net.sf.json.JSONObject; public class post { public static void main(String[] args) throws Exception { // String result = new Base64().getFromBase64(doPost()); System.out.println(doPost().getBytes()); } public static String doPost() throws Exception { login lg = new login(); JSONObject parameterData = lg.Res(); // JSONObject parameterData = lg.Res(); System.out.println(parameterData); URL localURL = new URL("http://***.wanlitong.com/"); URLConnection connection = localURL.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection)connection; httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.toString().length())); OutputStream outputStream = null; OutputStreamWriter outputStreamWriter = null; InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader reader = null; StringBuffer resultBuffer = new StringBuffer(); String tempLine = null; try { outputStream = httpURLConnection.getOutputStream(); outputStreamWriter = new OutputStreamWriter(outputStream); outputStreamWriter.write(parameterData.toString()); outputStreamWriter.flush(); if (httpURLConnection.getResponseCode() >= 300) { throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } inputStream = httpURLConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); reader = new BufferedReader(inputStreamReader); while ((tempLine = reader.readLine()) != null) { resultBuffer.append(tempLine); } } finally { if (outputStreamWriter != null) { outputStreamWriter.close(); } if (outputStream != null) { outputStream.close(); } if (reader != null) { reader.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (inputStream != null) { inputStream.close(); } } return resultBuffer.toString(); } }
post.java
package http; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class sha256 { public String Encrypt(String strSrc, String encName) { MessageDigest md = null; String strDes = null; byte[] bt = strSrc.getBytes(); try { if (encName == null || encName.equals("")) { encName = "SHA-256"; } md = MessageDigest.getInstance(encName); md.update(bt); strDes = bytes2Hex(md.digest()); // to HexString } catch (NoSuchAlgorithmException e) { return null; } // System.out.println(strDes); return strDes; } public static String bytes2Hex(byte[] bts) { String des = ""; String tmp = null; for (int i = 0; i < bts.length; i++) { tmp = (Integer.toHexString(bts[i] & 0xFF)); if (tmp.length() == 1) { des += "0"; } des += tmp; } return des; } }
sha256.java
时间: 2024-10-10 03:00:54