Skip to content

Commit 46cdee7

Browse files
committed
Fixed the wrong request url when directly using the url as path
1 parent 610f725 commit 46cdee7

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/main/java/org/javawebstack/httpclient/HTTPRequest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class HTTPRequest {
2121
private final QueryString query = new QueryString();
2222
private final Map<String, String> requestHeaders = new HashMap<>();
2323
private byte[] requestBody;
24-
private Map<String, String> responseHeaders = new HashMap<>();
24+
private final Map<String, String> responseHeaders = new HashMap<>();
2525
private byte[] responseBody;
2626
private int status;
2727

@@ -70,9 +70,6 @@ public HTTPRequest basicAuthorization(String username, String password){
7070
return authorization("Basic", Base64.getEncoder().encodeToString((username+":"+password).getBytes()));
7171
}
7272

73-
/*
74-
* TODO: Currently not supporting nested form-data
75-
* */
7673
public HTTPRequest formBody(Map<String, String> data){
7774
return body(new QueryString(data).toString());
7875
}
@@ -121,7 +118,7 @@ public String header(String key){
121118
public HTTPRequest execute(){
122119
HttpURLConnection conn = null;
123120
try{
124-
URL theUrl = new URL(client.getBaseUrl() + (path.startsWith("/") ? "" : "/") + path + (query.size() > 0 ? "?" + query.toString() : ""));
121+
URL theUrl = new URL(client.getBaseUrl() + ((path.startsWith("/") || path.startsWith("http://") || path.startsWith("https://")) ? "" : "/") + path + (query.size() > 0 ? "?" + query.toString() : ""));
125122
conn = (HttpURLConnection) theUrl.openConnection();
126123
conn.setReadTimeout(client.getTimeout());
127124
conn.setConnectTimeout(5000);
@@ -147,10 +144,12 @@ public HTTPRequest execute(){
147144
}else{
148145
this.responseBody = readAll(conn.getInputStream());
149146
}
147+
return this;
150148
}catch(Exception e){
151149
try {
152150
this.responseBody = readAll(conn.getErrorStream());
153-
}catch(IOException | NullPointerException ex){}
151+
return this;
152+
}catch(IOException | NullPointerException ignored){}
154153
}
155154
this.responseBody = new byte[0];
156155
return this;

0 commit comments

Comments
 (0)