如何解决为什么我在 HttpUrlConnection Java 中使用 httpStatus:401 或 httpStatus 500?
我尝试使用 HttpURLConnection 发布请求
这是请求正文:
这是请求头:
这是我的代码:
public static String postSms(long mNo,long cepNo,String mesaj){
String responseLine = null;
String url = getSmsUrl();
String authKey = getSmsAuthKey();
try {
URL s_url = new URL(url);
httpCon = (HttpURLConnection) s_url.openConnection();
if(authKey != null){
httpCon.setRequestProperty("yd-x-token",authKey);
}
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type","application/json; utf-8");
httpCon.setRequestProperty("Accept","application/json");
httpCon.setDoOutput(true);
int responseCode = httpCon.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
JSONObject msj = new JSONObject();
msj.put("toNumber",cepNo);
msj.put("smsText",mesaj);
try(OutputStream os = httpCon.getoutputStream()) {
byte[] input = msj.toString().getBytes("utf-8");
os.write(input,input.length);
}
try(BufferedReader br = new BufferedReader(
new InputStreamReader(httpCon.getInputStream(),"utf-8"))) {
StringBuilder response = new StringBuilder();
responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
}
}else return "ER|[sendNotification][Sms] <===> "+responseCode;
} catch (MalformedURLException e) {
log.error("ER|[sendNotification][Sms] <===> Hata "+e);
return "ER|[sendNotification][Sms] <===> Hata "+e;
} catch (IOException e) {
log.error("ER|[sendNotification][Sms] <===> Hata "+e);
return "ER|[sendNotification][Sms] <===> Hata "+e;
}catch (JSONException e){
}
return "OK";
}
当我像两张照片一样尝试邮递员时,它成功了。但是当我尝试使用 HttpURLConnection 时,响应代码 = 500。有时 responseCode 会出现 401,但是当他们提供随 httpstatus 500 更改的新令牌时。为什么我会收到此错误。
解决方法
如果您收到状态 401,则表示您无权访问此 URL。这就是为什么他们给了你一个新的令牌,你的状态是 500。所以在调用那个请求之前确保你总是有正确的令牌。 对于状态 500,这是服务器端的问题。告诉您的服务器开发人员,检查一下。 顺便说一句,这是一个以“.json”结尾的愚蠢网址。
,真正的方法是:
public static String postSms(long mNo,long cepNo,String mesaj){
String responseLine = null;
String url = getSmsUrl();
String authKey = getSmsAuthKey();
try {
URL s_url = new URL(url);
httpCon = (HttpURLConnection) s_url.openConnection();
if(authKey != null){
httpCon.setRequestProperty("yd-x-token",authKey);
}
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type","application/json; utf-8");
httpCon.setRequestProperty("Accept","application/json");
httpCon.setDoOutput(true);
JSONObject msj = new JSONObject();
msj.put("toNumber",cepNo);
msj.put("smsText",mesaj);
try(OutputStream os = httpCon.getOutputStream()) {
byte[] input = msj.toString().getBytes("utf-8");
os.write(input,input.length);
}
int responseCode = httpCon.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
try(BufferedReader br = new BufferedReader(
new InputStreamReader(httpCon.getInputStream(),"utf-8"))) {
StringBuilder response = new StringBuilder();
responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
}
}else return "ER|[sendNotification][Sms] <===> "+responseCode;
} catch (MalformedURLException e) {
log.error("ER|[sendNotification][Sms] <===> Hata "+e);
return "ER|[sendNotification][Sms] <===> Hata "+e;
} catch (IOException e) {
log.error("ER|[sendNotification][Sms] <===> Hata "+e);
return "ER|[sendNotification][Sms] <===> Hata "+e;
}catch (JSONException e){
}
return "OK";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。