· 6 years ago · Feb 27, 2020, 07:48 PM
1// allm�nt tillg�nglig p� n�tet p� https://www.mkyong.com/java/java-https-client-httpsurlconnection-example/
2
3
4// my API key: 9M7L1UZCEUWT8I5U
5package programmering3;
6
7import java.net.MalformedURLException;
8import java.net.URL;
9import java.security.cert.Certificate;
10import java.util.Arrays;
11import java.io.*;
12
13import javax.net.ssl.HttpsURLConnection;
14import javax.net.ssl.SSLPeerUnverifiedException;
15
16import org.json.JSONObject;
17
18public class HttpsClient{
19
20 public static void main(String[] args)
21 {
22 new HttpsClient().testIt();
23 }
24
25 public StringBuffer testIt(){ // <--- skapar metod som returnerar stringbuffer
26 String https_url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=15min&outputsize=full&apikey=9M7L1UZCEUWT8I5U";
27 URL url;
28
29
30 JSONObject output = new JSONObject();
31
32 try {
33
34 url = new URL(https_url);
35 HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
36
37 //dumpl all cert info
38 print_https_cert(con);
39
40 //dump all the content
41 InputStream input = new URL(https_url).openStream(); // <--- lägger url:en i en input
42
43 BufferedReader in = new BufferedReader(new InputStreamReader(input)); // <---läser in från input
44 StringBuffer str = new StringBuffer(); // <---ny stringbuffer
45 String strr;
46 while ((strr = in.readLine())!= null) { //Här lägger vi till input från in, alltså bufferedreaderinstansen
47
48 str.append(strr + "\n");
49 }
50 JSONObject a = new JSONObject(str.toString()); //JSonobject som tar stringbuffer som argument
51 output = a;
52 String[] keys = JSONObject.getNames(output); //Get an array of field names from a JSONObject.. rad 812
53
54 JSONObject j1 = output.getJSONObject(keys[0]);
55 String[] s1 = JSONObject.getNames(j1);
56
57 JSONObject j2 =j1.getJSONObject(s1[0]);
58 String [] s2 = JSONObject.getNames(j2);
59
60 Object j3 = j2.get(s2[0]);
61 String[] s3 = new String[s2.length];
62 for(int l=0; l<= s2.length-1; l++) {
63 s3[l] =j2.getString(s2[l]);
64 System.out.println("s3 : " + s3[l]);
65 }
66
67
68 for(int i= 0; i<=keys.length-1; i++){
69 for(int j=0; j<=s1.length-1; j++){
70 for(int k=0; k<= s2.length-1; k++) {
71 for(int l=0; l<=s3.length-1; l++) {
72 System.out.println("Date: " + s1[j] + " " + s2[k] + " " +s3[l]);
73 }
74
75 }
76 }
77 }
78
79
80
81
82// output.remove("Meta Data");
83// String[] keys = JSONObject.getNames(output); //Get an array of field names from a JSONObject.. rad 812
84// JSONObject ts = output.getJSONObject(keys[0]); //Get the JSONObject value associated with a key., rad 773
85// String[] out = JSONObject.getNames(ts);
86// Arrays.sort(out);
87 //vi lagrar alla m�jliga strings i en array s� att vi l�tt kan komma �t
88 //�nskade dataserie senare
89
90 //har nu kommandon för att hämta både keys och values. få sen att printa dem ut
91
92
93
94
95 /*
96 *
97
98 for (int i =0; i < test.length; i++) {
99 test[i] = "==== Showing data for " + dataCBox.getItemAt(i) + " =====" + "\n";
100 }
101
102
103 for (int y = 0; y < test.length; y++) {
104 for (int i = 0; i < out.length; i++) {
105 test[y] += out[i] + ": " +
106 ts.getJSONObject(out[i]).getDouble((String)dataCBox.getItemAt(y)) +"\n";
107 }
108 }
109 TextBox.textArea.setText(test[dataCBox.getSelectedIndex()]);
110 */
111 return str;
112
113 } catch (MalformedURLException e) {
114
115 e.printStackTrace();
116 return null;
117 } catch (IOException e) {
118 e.printStackTrace();
119 return null;
120 }
121
122 }
123
124 private void print_https_cert(HttpsURLConnection con){
125
126 if(con!=null){
127
128 try {
129
130 System.out.println("Response Code : " + con.getResponseCode());
131 System.out.println("Cipher Suite : " + con.getCipherSuite());
132 System.out.println("\n");
133
134 Certificate[] certs = con.getServerCertificates();
135 for(Certificate cert : certs){
136 System.out.println("Cert Type : " + cert.getType());
137 System.out.println("Cert Hash Code : " + cert.hashCode());
138 System.out.println("Cert Public Key Algorithm : "
139 + cert.getPublicKey().getAlgorithm());
140 System.out.println("Cert Public Key Format : "
141 + cert.getPublicKey().getFormat());
142 System.out.println("\n");
143 }
144
145 } catch (SSLPeerUnverifiedException e) {
146 e.printStackTrace();
147 } catch (IOException e){
148 e.printStackTrace();
149 }
150
151 }
152
153 }
154
155 private void print_content(HttpsURLConnection con){
156 if(con!=null){
157
158
159 try {
160
161 System.out.println("****** Content of the URL ********");
162 BufferedReader br =
163 new BufferedReader(
164 new InputStreamReader(con.getInputStream()));
165
166 String input;
167
168 while ((input = br.readLine()) != null){
169 System.out.println(input);
170 }
171 br.close();
172
173 } catch (IOException e) {
174 e.printStackTrace();
175 }
176
177 }
178
179 }
180
181}