api經過抓包獲取java
javapython
1.get和post方法apache
package com.tools.httpUtil; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; public class HttpRequest { public static String doGet(String url,String cookie) { // 輸入流 InputStream is = null; BufferedReader br = null; String result = null; // 建立httpClient實例 HttpClient httpClient = new HttpClient(); // 設置http鏈接主機服務超時時間:15000毫秒 // 先獲取鏈接管理器對象,再獲取參數對象,再進行參數的賦值 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000); // 建立一個Get方法實例對象 GetMethod getMethod = new GetMethod(url); getMethod.setRequestHeader("cookie",cookie); // 設置get請求超時爲60000毫秒 getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000); // 設置請求重試機制,默認重試次數:3次,參數設置爲true,重試機制可用,false相反 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true)); try { // 執行Get方法 int statusCode = httpClient.executeMethod(getMethod); // 判斷返回碼 if (statusCode != HttpStatus.SC_OK) { // 若是狀態碼返回的不是ok,說明失敗了,打印錯誤信息 System.err.println("Method faild: " + getMethod.getStatusLine()); } else { // 經過getMethod實例,獲取遠程的一個輸入流 is = getMethod.getResponseBodyAsStream(); // 包裝輸入流 br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sbf = new StringBuffer(); // 讀取封裝的輸入流 String temp = null; while ((temp = br.readLine()) != null) { sbf.append(temp).append("\r\n"); } result = sbf.toString(); } } catch (IOException e) { e.printStackTrace(); } finally { // 關閉資源 if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } // 釋放鏈接 getMethod.releaseConnection(); } return result; } public static StringBuffer doPost(String url, Map<String, Object> paramMap) { // 獲取輸入流 StringBuffer tmpcookies = new StringBuffer(); InputStream is = null; BufferedReader br = null; String result = null; // 建立httpClient實例對象 HttpClient httpClient = new HttpClient(); // 設置 HttpClient 接收 Cookie,用與瀏覽器同樣的策略 httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // 設置httpClient鏈接主機服務器超時時間:15000毫秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000); // 建立post請求方法實例對象 PostMethod postMethod = new PostMethod(url); // 設置post請求超時時間 // postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000); NameValuePair[] nvp = null; // 判斷參數map集合paramMap是否爲空 if (null != paramMap && paramMap.size() > 0) {// 不爲空 // 建立鍵值參數對象數組,大小爲參數的個數 nvp = new NameValuePair[paramMap.size()]; // 循環遍歷參數集合map Set<Entry<String, Object>> entrySet = paramMap.entrySet(); // 獲取迭代器 Iterator<Entry<String, Object>> iterator = entrySet.iterator(); int index = 0; while (iterator.hasNext()) { Entry<String, Object> mapEntry = iterator.next(); // 從mapEntry中獲取key和value建立鍵值對象存放到數組中 try { nvp[index] = new NameValuePair(mapEntry.getKey(), new String(mapEntry.getValue().toString().getBytes("UTF-8"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } index++; } } // 判斷nvp數組是否爲空 if (null != nvp && nvp.length > 0) { // 將參數存放到requestBody對象中 postMethod.setRequestBody(nvp); } // 執行POST方法 try { int statusCode = httpClient.executeMethod(postMethod); // 判斷是否成功 if (statusCode != HttpStatus.SC_OK) { System.err.println("Method faild: " + postMethod.getStatusLine()); } Cookie[] cookies = httpClient.getState().getCookies(); for (Cookie c : cookies) { tmpcookies.append(c.toString() + ";"); System.out.println("cookies = "+c.toString()); } } catch (IOException e) { e.printStackTrace(); } finally { // 關閉資源 if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } // 釋放鏈接 postMethod.releaseConnection(); } return tmpcookies; } }
2.獲取issuesjson
package com.tools.httpUtil; import java.util.HashMap; import java.util.Map; public class SonarHttp { public String getIssues(String key,String type){ Map<String,Object> user=new HashMap<String, Object>(); user.put("login","**"); user.put("password","**$"); HttpRequest request=new HttpRequest(); StringBuffer cookie=request.doPost("http://sonar.*.com/api/authentication/login",user); String url="http://sonar.*.com/api/issues/search?componentKeys=%s&s=FILE_LINE&resolved=false&types=%s&ps=100&facets=severities,types&additionalFields=_all"; String str=request.doGet(url.format(key, type),cookie.toString()); return (str); } }
2、pythonapi
一、數組
#coding:utf-8 import cookielib import json import logging import urllib import urllib2 import time from security_platform.config import ldap_user, ldap_passwd, get_issues_url def login(): try: login_url="http://sonar.*.com/api/authentication/login" values={"login":ldap_user,"password":ldap_passwd} data = urllib.urlencode(values) user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0' headers = {'User-Agent': user_agent, 'Content-type': "application/x-www-form-urlencoded","Accept":"application/json", "Accept-Encoding":"gzip, deflate","Connection":"keep-alive","Content-Length":"33"} req = urllib2.Request(login_url, data=data, headers=headers) return req except Exception as e: logging.error(e) def do_get(url): logging.info("調用sonar接口開始》》》") start=time.time() cookieJar = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar)) req=login() opener.open(req) result = opener.open(url) res = result.read() end=time.time() logging.info("調用sonar接口api/issues/search結束,用時:"+str(end-start)) return res