JSP的幾個小結

1.sha256哈希加密
1.1利用 java.security.MessageDigest 調用已經集成的 Hash 算法
<%@ page import="java.security.MessageDigest" %>
<%@ page import="java.security.NoSuchAlgorithmException" %>
strText = serialnum+genotype+result_type+salt;
               //out.println("<br/>"+strText);
               try{  
                  // SHA 加密開始  
                  // 創建加密對象 並傳入加密類型  
                  MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");  
                  // 傳入要加密的字符串  
                  messageDigest.update(strText.getBytes());  
                  // 得到 byte 類型結果  
                  byte byteBuffer[] = messageDigest.digest();  
                  // 將 byte 轉換爲 string  
                  StringBuffer strHexString = new StringBuffer();  
                  // 遍歷 byte buffer  
                  for (int l = 0; l < byteBuffer.length; l++){  
                     String hex = Integer.toHexString(0xff & byteBuffer[l]);  
                     if (hex.length() == 1){  
                        strHexString.append('0');  
                        }  
                     strHexString.append(hex);  
                  }  
                  // 得到返回結果  
                  token = strHexString.toString();  
               }catch (NoSuchAlgorithmException e){  
                     e.printStackTrace();  
               }

參考鏈接:JAVA 的 SHA-256 和 SHA-512 兩種 Hash 算法的調用

1.2使用apache的commons-codec庫來做

<%@ page import="org.apache.commons.codec.digest.DigestUtils" %>
<%@ page import="org.apache.commons.codec.binary.Hex" %>

String text = "123456123456";
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(text.getBytes("UTF-8"));
            String output = Hex.encodeHexString(hash);
            System.out.println(output);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

參考鏈接:使用apache的commons-codec庫進行SHA256哈希

2.使用httpclient發送post請求

需要導入的包:common-httpclient,common-logging,common-codec
<%@ page import="org.apache.commons.httpclient.*" %>
<%@ page import="org.apache.commons.httpclient.methods.PostMethod" %>
<%@ page import="org.apache.commons.httpclient.params.HttpMethodParams" %>

 //第一步:創建HttpClient對象  
             HttpClient httpClient = new HttpClient();
             String url = "http://testcms.ihbaby.com/gene/result.do";
             //out.println(url);
             //第二步:生成使用POST方法的請求對象,參數是訪問的服務器地址
             PostMethod postMethod = new PostMethod(url);
             //NameValuePair對象代表了一個需要發往服務器的鍵值對
             NameValuePair[] data = new NameValuePair[4];  
             data[0] = new NameValuePair("serialnum", serialnum);
             data[1] = new NameValuePair("genotype", genotype);
             data[2] = new NameValuePair("result_type", result_type);
             data[3] = new NameValuePair("token", token);
             // 將表單的值放入postMethod中
             postMethod.setRequestBody(data);
            // 執行postMethod
            int statusCode = httpClient.executeMethod(postMethod);
            //out.println("<br/>"+data[3].toString());
            //out.println("<br/>"+statusCode);
            //out.println("<br/>"+postMethod.getResponseBodyAsString());

參考鏈接:HttpClient入門

3.java處理json字符串

使用postMethod.getResponseBodyAsString()得到的相應消息如下:

 {
    "result": {
        "status": 0,
        "msg": null,
        "data": null,
        "msgMap": null
    }
}

該消息爲json字符串,需要進行解析。
比較簡單的方法是使用org.json庫
<%@ page import="org.json.JSONObject" %>

JSONObject datajson = new JSONObject(postMethod.getResponseBodyAsString());
            status = datajson.getJSONObject("result").getString("status");
            //out.println("<br/>"+status);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章