gpt4 book ai didi

android - HttpUrlConnection getResponseCode 抛出 IOException

转载 作者:行者123 更新时间:2023-11-29 01:41:06 24 4
gpt4 key购买 nike

当我尝试调用 getCodeResponse() 时出现 IOException。当参数有效时没有异常,代码响应为 200。如果参数错误,服务器应返回 401 代码。我已经在 hurl.it 上测试了查询,如果参数错误,我会收到 401 代码。可能 HttpURLConnection 类在出现错误代码时抛出异常。

        URL url = new URL(sUrl);
String charset = "UTF-8";
conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(DEFAULT_TIMEOUT /* milliseconds */);
conn.setConnectTimeout(DEFAULT_TIMEOUT /* milliseconds */);
conn.setRequestMethod(methodType);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.addRequestProperty("Accept-Charset", charset);
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.addRequestProperty(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);

conn.setDoOutput(true);

conn.setInstanceFollowRedirects(false);

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os));

writer.write(query);
writer.flush();
writer.close();
//os.write(query.getBytes());
os.flush();
os.close();
conn.connect();

if(conn != null){

responseCode = conn.getResponseCode();
result = readResponse(conn);
}

最佳答案

IOException 表示有 401 响应。打印堆栈跟踪,如果其他一切正确,它会给出 401 响应。类似于:java.io.IOException:服务器返回 HTTP 响应代码:URL 的 401:

关于android - HttpUrlConnection getResponseCode 抛出 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24630261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com