25
25
import java .io .ByteArrayOutputStream ;
26
26
import java .io .IOException ;
27
27
import java .io .InputStream ;
28
- import java .net .HttpURLConnection ;
29
28
import java .net .MalformedURLException ;
30
29
import java .net .Proxy ;
31
30
import java .net .URL ;
@@ -62,14 +61,9 @@ public JSONObject getFromKeybase(String path, String query) throws KeybaseExcept
62
61
String url = connectionClient .getKeybaseBaseUrl () + path + URLEncoder .encode (query , "utf8" );
63
62
64
63
URL realUrl = new URL (url );
65
-
66
- HttpURLConnection conn = (HttpURLConnection ) connectionClient .openConnection (realUrl , proxy , true );
67
- conn .connect ();
68
-
69
- int response = conn .getResponseCode ();
70
-
71
- if (response >= 200 && response < 300 ) {
72
- String text = snarf (conn .getInputStream ());
64
+ KeybaseUrlConnectionClient .Response response = connectionClient .getUrlResponse (realUrl , proxy , true );
65
+ if (response .getCode () >= 200 && response .getCode () < 300 ) {
66
+ String text = snarf (response .getStream ());
73
67
try {
74
68
JSONObject json = new JSONObject (text );
75
69
if (JWalk .getInt (json , "status" , "code" ) != 0 ) {
@@ -81,7 +75,7 @@ public JSONObject getFromKeybase(String path, String query) throws KeybaseExcept
81
75
throw KeybaseException .keybaseScrewup (e );
82
76
}
83
77
} else {
84
- String message = snarf ( conn . getErrorStream () );
78
+ String message = response . getMessage ( );
85
79
throw KeybaseException .networkScrewup ("api.keybase.io query error (status=" + response + "): " + message );
86
80
}
87
81
} catch (Exception e ) {
@@ -103,27 +97,23 @@ public Fetch fetchProof(String urlString) {
103
97
Fetch result = new Fetch ();
104
98
105
99
try {
106
- HttpURLConnection conn = null ;
107
- int status = 0 ;
100
+ KeybaseUrlConnectionClient .Response response = null ;
108
101
int redirects = 0 ;
109
102
while (redirects < REDIRECT_TRIES ) {
110
103
result .mActualUrl = urlString ;
111
104
URL url = new URL (urlString );
112
- conn = (HttpURLConnection ) connectionClient .openConnection (url , proxy , false );
113
- conn .addRequestProperty ("User-Agent" , "Keybase Java client, github.com/timbray/KeybaseLib" );
114
- conn .connect ();
115
- status = conn .getResponseCode ();
116
- if (status == 301 ) {
105
+ response = connectionClient .getUrlResponse (url , proxy , false );
106
+ if (response .getCode () == 301 ) {
117
107
redirects ++;
118
- urlString = conn . getHeaderFields ().get ("Location" ).get (0 );
108
+ urlString = response . getHeaders ().get ("Location" ).get (0 );
119
109
} else {
120
110
break ;
121
111
}
122
112
}
123
- if (status >= 200 && status < 300 ) {
124
- result .mBody = KeybaseQuery .snarf (conn . getInputStream ());
113
+ if (response . getCode () >= 200 && response . getCode () < 300 ) {
114
+ result .mBody = KeybaseQuery .snarf (response . getStream ());
125
115
} else {
126
- result .mProblem = "Fetch failed, status " + status + ": " + KeybaseQuery . snarf ( conn . getErrorStream () );
116
+ result .mProblem = "Fetch failed, status " + response . getCode () + ": " + response . getMessage ( );
127
117
}
128
118
129
119
} catch (MalformedURLException e ) {
0 commit comments