1
1
package javaxt .azure .graph ;
2
2
3
+ import java .util .*;
4
+ import java .net .http .HttpClient ;
5
+ import java .net .http .HttpRequest ;
6
+ import java .net .http .HttpResponse ;
7
+ import static java .nio .charset .StandardCharsets .*;
8
+
3
9
import javaxt .json .*;
4
10
import static javaxt .utils .Console .console ;
5
11
@@ -100,13 +106,34 @@ public JSONObject getResponse(String url, JSONObject payload) throws Exception {
100
106
//**************************************************************************
101
107
public JSONObject getResponse (String url , JSONObject payload , String method ) throws Exception {
102
108
103
- javaxt .http .Request request = getRequest (url );
104
- if (method !=null ) request .setRequestMethod (method );
105
- if (payload !=null ) request .write (payload );
106
109
107
- javaxt .http .Response response = request .getResponse ();
108
- JSONObject json = response .getJSONObject ();
109
- int status = response .getStatus ();
110
+ //Get request builder
111
+ HttpRequest .Builder request = getRequest (url );
112
+
113
+
114
+ //Set payload and method as needed
115
+ if (payload !=null ){
116
+ if (method ==null ) method = "POST" ;
117
+ else method = method .toUpperCase ();
118
+
119
+ request .header ("Content-Type" , "application/json;charset=UTF-8" );
120
+ request .method (method , HttpRequest .BodyPublishers .ofByteArray (
121
+ payload .toString ().getBytes (UTF_8 ))
122
+ );
123
+ }
124
+
125
+
126
+ //Get response
127
+ HttpResponse <String > response = HttpClient .newBuilder ()
128
+ .followRedirects (HttpClient .Redirect .NEVER )
129
+ .build ()
130
+ .send (request .build (), HttpResponse .BodyHandlers .ofString (UTF_8 ));
131
+
132
+
133
+
134
+ //Parse and return response
135
+ JSONObject json = new JSONObject (response .body ());
136
+ int status = response .statusCode ();
110
137
if (status ==200 ){
111
138
return json ;
112
139
}
@@ -115,17 +142,19 @@ else if (status==429){
115
142
return getResponse (url );
116
143
}
117
144
else {
118
- System .out .println (response .toString ());
145
+
146
+ //console.log(response.headers().map());
147
+ //System.out.println(response.toString());
119
148
System .out .println (json .toString (4 ));
120
149
throw new Exception ();
121
150
}
122
151
}
123
152
124
153
125
154
//**************************************************************************
126
- //** get
155
+ //** getRequest
127
156
//**************************************************************************
128
- private synchronized javaxt . http . Request getRequest (String url ) throws Exception {
157
+ private synchronized HttpRequest . Builder getRequest (String url ) throws Exception {
129
158
130
159
//Update url as needed
131
160
if (!url .startsWith (graphURL ) && !url .startsWith ("http" )){
@@ -141,10 +170,9 @@ private synchronized javaxt.http.Request getRequest(String url) throws Exception
141
170
142
171
143
172
//Execute http request and return response
144
- javaxt .http .Request request = new javaxt .http .Request (url );
145
- request .setHeader ("Authorization" , tokenType + " " + accessToken );
146
- request .setNumRedirects (0 );
147
- return request ;
173
+ return HttpRequest .newBuilder ()
174
+ .uri (java .net .URI .create (url ))
175
+ .header ("Authorization" , tokenType + " " + accessToken );
148
176
}
149
177
150
178
}
0 commit comments