|
| 1 | +package console; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Map.Entry; |
| 13 | + |
| 14 | +import org.apache.http.Header; |
| 15 | +import org.apache.http.HeaderIterator; |
| 16 | +import org.apache.http.HttpEntity; |
| 17 | +import org.apache.http.HttpResponse; |
| 18 | +import org.apache.http.NameValuePair; |
| 19 | +import org.apache.http.ParseException; |
| 20 | +import org.apache.http.client.ClientProtocolException; |
| 21 | +import org.apache.http.client.entity.UrlEncodedFormEntity; |
| 22 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 23 | +import org.apache.http.client.methods.HttpGet; |
| 24 | +import org.apache.http.client.methods.HttpPost; |
| 25 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 26 | +import org.apache.http.impl.client.HttpClients; |
| 27 | +import org.apache.http.message.BasicNameValuePair; |
| 28 | +import org.apache.http.util.EntityUtils; |
| 29 | + |
| 30 | +import us.codecraft.webmagic.selector.XpathSelector; |
| 31 | + |
| 32 | +public class HttpClientExample { |
| 33 | + private List<String> ProblemDescriptionPath = new ArrayList<String>(); |
| 34 | + private List<String> ProblemSubmissionPath = new ArrayList<String>(); |
| 35 | + private Map<String, List<String>> problemCodePath = new HashMap<String, List<String>>(); |
| 36 | + |
| 37 | + public static void main(String[] args) throws ClientProtocolException, |
| 38 | + IOException { |
| 39 | + CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 40 | + HttpGet httpGet = new HttpGet("https://leetcode.com"); |
| 41 | + CloseableHttpResponse response1 = httpClient.execute(httpGet); |
| 42 | +// printResponse(response1); |
| 43 | + String cookieString = getCookie(response1); |
| 44 | + HttpEntity entity1 = response1.getEntity(); |
| 45 | + EntityUtils.consume(entity1); |
| 46 | + |
| 47 | + HttpPost httpPost = new HttpPost("https://leetcode.com/accounts/login/"); |
| 48 | + httpPost.addHeader( |
| 49 | + "User-Agent", |
| 50 | + "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6)Gecko/20091201 Firefox/3.5.6"); |
| 51 | + httpPost.addHeader("Referer", "https://leetcode.com/accounts/login/"); |
| 52 | + httpPost.addHeader("Origin", "https://leetcode.com"); |
| 53 | + Map<String, String> map = new HashMap<String, String>(); |
| 54 | + map.put("login", "tanghaodong25@163.com"); |
| 55 | + map.put("password", "thd04180015"); |
| 56 | + map.put("csrfmiddlewaretoken", cookieString); |
| 57 | + map.put("remember", "on"); |
| 58 | + httpPost.setEntity(new UrlEncodedFormEntity(getParam(map), "UTF-8")); |
| 59 | + response1 = httpClient.execute(httpPost); |
| 60 | + HeaderIterator iterator = response1.headerIterator(); |
| 61 | +// while (iterator.hasNext()) { |
| 62 | +// iterator.next(); |
| 63 | +// } |
| 64 | +// printResponse(response1); |
| 65 | + entity1 = response1.getEntity(); |
| 66 | + EntityUtils.consume(entity1); |
| 67 | + |
| 68 | + HttpGet httpGet1 = new HttpGet( |
| 69 | + "https://leetcode.com/problems/rotate-array/submissions/"); |
| 70 | + response1 = httpClient.execute(httpGet1); |
| 71 | + printResponse(response1); |
| 72 | + entity1 = response1.getEntity(); |
| 73 | + EntityUtils.consume(entity1); |
| 74 | + } |
| 75 | + |
| 76 | + public static List<NameValuePair> getParam(Map parameterMap) { |
| 77 | + List<NameValuePair> param = new ArrayList<NameValuePair>(); |
| 78 | + Iterator it = parameterMap.entrySet().iterator(); |
| 79 | + while (it.hasNext()) { |
| 80 | + Entry parmEntry = (Entry) it.next(); |
| 81 | + param.add(new BasicNameValuePair((String) parmEntry.getKey(), |
| 82 | + (String) parmEntry.getValue())); |
| 83 | + } |
| 84 | + return param; |
| 85 | + } |
| 86 | + @SuppressWarnings("unused") |
| 87 | + public static void printResponse(HttpResponse httpResponse) |
| 88 | + throws ParseException, IOException { |
| 89 | + // 获取响应消息实体 |
| 90 | + |
| 91 | + HttpEntity entity = httpResponse.getEntity(); |
| 92 | + // 响应状态 |
| 93 | + System.out.println("status:" + httpResponse.getStatusLine()); |
| 94 | + System.out.println("headers:"); |
| 95 | + HeaderIterator iterator = httpResponse.headerIterator(); |
| 96 | + while (iterator.hasNext()) { |
| 97 | + System.out.println("\t" + iterator.next()); |
| 98 | + } |
| 99 | + // 判断响应实体是否为空 |
| 100 | + // if (entity != null) { |
| 101 | + // String responseString = EntityUtils.toString(entity); |
| 102 | + // System.out.println("response length:" + responseString.length()); |
| 103 | + // System.out.println("response content:" |
| 104 | + // + responseString.replace("\r\n", "")); |
| 105 | + // } |
| 106 | + InputStream responseBody = httpResponse.getEntity().getContent(); |
| 107 | + StringBuilder stringBuilder = new StringBuilder(); |
| 108 | + BufferedReader br = new BufferedReader(new InputStreamReader( |
| 109 | + responseBody)); |
| 110 | + String line = null; |
| 111 | + while ((line = br.readLine()) != null) { |
| 112 | + stringBuilder.append("\n"+line); |
| 113 | + } |
| 114 | + System.out.println(stringBuilder.toString()); |
| 115 | + XpathSelector xpathSelector = new XpathSelector("//table[@id='result_testcases']/tbody/tr/td/a[@class'status-accepted text-success']/@href"); |
| 116 | + System.out.println(xpathSelector.selectList(stringBuilder.toString())); |
| 117 | + } |
| 118 | + |
| 119 | + public static String getCookie(HttpResponse httpResponse) { |
| 120 | + Header[] headers = httpResponse.getAllHeaders(); |
| 121 | + for (Header value : headers) { |
| 122 | + if (value.getName().equals("Set-Cookie")) { |
| 123 | + return value.getValue().split(";")[0].split("=")[1]; |
| 124 | + } |
| 125 | + } |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments