Skip to content

Commit 47aff96

Browse files
committed
Updated the getRequest() and getResponse() methods in the Connection class to use classes in the java.net.http package
1 parent a2f9a70 commit 47aff96

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

src/javaxt/azure/graph/Connection.java

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package javaxt.azure.graph;
22

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+
39
import javaxt.json.*;
410
import static javaxt.utils.Console.console;
511

@@ -100,13 +106,34 @@ public JSONObject getResponse(String url, JSONObject payload) throws Exception {
100106
//**************************************************************************
101107
public JSONObject getResponse(String url, JSONObject payload, String method) throws Exception {
102108

103-
javaxt.http.Request request = getRequest(url);
104-
if (method!=null) request.setRequestMethod(method);
105-
if (payload!=null) request.write(payload);
106109

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();
110137
if (status==200){
111138
return json;
112139
}
@@ -115,17 +142,19 @@ else if (status==429){
115142
return getResponse(url);
116143
}
117144
else{
118-
System.out.println(response.toString());
145+
146+
//console.log(response.headers().map());
147+
//System.out.println(response.toString());
119148
System.out.println(json.toString(4));
120149
throw new Exception();
121150
}
122151
}
123152

124153

125154
//**************************************************************************
126-
//** get
155+
//** getRequest
127156
//**************************************************************************
128-
private synchronized javaxt.http.Request getRequest(String url) throws Exception {
157+
private synchronized HttpRequest.Builder getRequest(String url) throws Exception {
129158

130159
//Update url as needed
131160
if (!url.startsWith(graphURL) && !url.startsWith("http")){
@@ -141,10 +170,9 @@ private synchronized javaxt.http.Request getRequest(String url) throws Exception
141170

142171

143172
//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);
148176
}
149177

150178
}

0 commit comments

Comments
 (0)