在向API发出请求时遇到了一些麻烦,它给我一个401错误。 该请求是通过使用令牌进行的,并且我使用相同的令牌发出了其他几个请求,并且它工作正常,它是'只是那个特殊的要求。 为了完成,我试图用邮递员做这个请求,它完全正常。
- 编辑 -
private void run() {
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
URL url = new URL(urlStr);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Configuring connection properties
conn.setReadTimeout(150000);
conn.setConnectTimeout(150000);
conn.setRequestMethod(requestType);
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(params));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
Log.d("HTTP", "Code: " + responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
while ((response = br.readLine()) != null) {
sb.append(response);
}
}
if (!(sb.toString().isEmpty()))
this.response = sb.toString();
else
{
this.response = queryResponse.WRONG;
}
} catch (MalformedURLException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
}