Skip to content

Commit 1389b83

Browse files
committed
re-authenticate before creating request to avoid 'already connected' error on POST
1 parent f174a6a commit 1389b83

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public String createUrl(Object requestDto, Map<String,String> query){
136136
}
137137

138138
public HttpURLConnection createRequest(String requestUrl, String httpMethod, byte[] requestBody, String requestType) {
139+
return createRequest(requestUrl, httpMethod, requestBody, requestType, false);
140+
141+
}
142+
143+
public HttpURLConnection createRequest(String requestUrl, String httpMethod, byte[] requestBody, String requestType, Boolean forceAuthentication) {
139144
try {
140145
URL url = new URL(requestUrl);
141146

@@ -153,7 +158,7 @@ public HttpURLConnection createRequest(String requestUrl, String httpMethod, byt
153158
req.setRequestProperty(HttpHeaders.ContentType, requestType);
154159
}
155160

156-
if (alwaysSendBasicAuthHeaders) {
161+
if (forceAuthentication || alwaysSendBasicAuthHeaders) {
157162
addBasicAuth(req, userName, password);
158163
}
159164

@@ -336,8 +341,8 @@ public <TResponse> TResponse send(String requestUrl, String httpMethod, byte[] r
336341

337342
if (shouldAuthenticate(req, userName, password)){
338343
req.disconnect();
339-
req = createRequest(requestUrl, httpMethod, requestBody, requestType);
340-
addBasicAuth(req, userName, password);
344+
req = createRequest(requestUrl, httpMethod, requestBody, requestType, true);
345+
341346
success = req.getResponseCode() < 400;
342347
}
343348

src/AndroidClient/client/src/test/java/net/servicestack/client/TestAuthTests.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,33 @@ public void test_does_send_BasicAuthHeaders(){
3535
assertNotNull(response.getSessionId());
3636
}
3737

38-
public void test_does_transparently_send_BasicAuthHeader_on_401_response(){
38+
private void does_transparently_send_BasicAuthHeader_on_401(Boolean isPost){
3939
ServiceClient client = CreateClient();
4040
client.setCredentials("test", "test");
41-
42-
testdtos.TestAuthResponse response = client.get(new testdtos.TestAuth());
43-
41+
42+
testdtos.TestAuthResponse response = null;
43+
if (isPost) {
44+
response = client.post(new testdtos.TestAuth());
45+
} else {
46+
response = client.get(new testdtos.TestAuth());
47+
}
4448
assertEquals("1", response.getUserId());
4549
assertEquals("test", response.getUserName());
4650
assertEquals("test DisplayName", response.getDisplayName());
4751
assertNotNull(response.getSessionId());
4852
}
53+
54+
public void test_does_transparently_send_BasicAuthHeader_on_401_response_From_get(){
55+
56+
does_transparently_send_BasicAuthHeader_on_401( false);
57+
58+
}
59+
60+
public void test_does_transparently_send_BasicAuthHeader_on_401_response_from_post(){
61+
62+
does_transparently_send_BasicAuthHeader_on_401( true);
63+
64+
}
4965

5066
public void test_can_authenticate_with_CredentialsAuth(){
5167
ServiceClient client = CreateClient();

0 commit comments

Comments
 (0)