response.getStatusLine().getStatusCode()
时间: 2024-04-15 22:29:40 浏览: 30
`response.getStatusLine().getStatusCode()` 是用于获取 HTTP 响应的状态码的方法。它属于 Java 中的 `HttpResponse` 类,用于获取 HTTP 请求的返回状态。
以下是示例代码,展示如何使用该方法获取状态码:
```
HttpResponse response = ...; // 获取 HTTP 响应对象
int statusCode = response.getStatusLine().getStatusCode(); // 获取状态码
System.out.println("Status Code: " + statusCode);
```
状态码是一个三位数的数字,表示了请求的处理结果。常见的状态码包括:
- 200:请求成功
- 404:请求的资源不存在
- 500:服务器内部错误
- 302:临时重定向
- 403:禁止访问
你可以根据不同的状态码进行相应的处理逻辑。
相关问题
public static String getHtmlCode(String path) { String result = null; try { HttpClient httpclient = getHttpClient(); HttpGet get = new HttpGet(path); HttpResponse response = httpclient.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(), "utf-8")); String line; result = ""; while ((line = br.readLine()) != null) { result += line + "\n"; } } } catch(ConnectTimeoutException e) { System.out.println("ConnectTimeoutException timeout"); return null; } catch (SocketTimeoutException e) { System.out.println("SocketTimeoutException timeout"); return null; } catch (Exception e) { System.out.println(e.getMessage() + ":" + e.toString()); return null; } return result; } }我哪里错了?修改结果
这段代码也是使用的过时的HTTPClient,同样可能会导致编译错误。你可以使用OkHttp来实现相同的功能,如下所示:
```
public static String getHtmlCode(String path) {
String result = null;
OkHttpClient client = getHttpClient();
try {
Request request = new Request.Builder()
.url(path)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
result = response.body().string();
}
} catch (ConnectTimeoutException e) {
System.out.println("ConnectTimeoutException timeout");
return null;
} catch (SocketTimeoutException e) {
System.out.println("SocketTimeoutException timeout");
return null;
} catch (Exception e) {
System.out.println(e.getMessage() + ":" + e.toString());
return null;
}
return result;
}
```
这个代码使用OkHttp的Request和Response对象来获取HTML代码,并且也支持设置连接和读取超时时间。使用OkHttp可以更加方便地实现HTTP请求,同时也支持HTTP/2和HTTP/1.1协议。
public static JSONObject publishDmpCustomAudience() { String access_token = "xxx"; final Long advertiser_id = 0L; // 请求地址 String open_api_url_prefix = "https://ad.oceanengine.com/open_api/2/"; String uri = "dmp/custom_audience/publish/"; // 请求参数 Map data = new HashMap() { { put("advertiser_id", advertiser_id); put("custom_audience_id", 3); } }; // 构造请求 HttpPost httpEntity = new HttpPost(open_api_url_prefix + uri); httpEntity.setHeader("Access-Token", access_token); CloseableHttpResponse response = null; CloseableHttpClient client = null; try { client = HttpClientBuilder.create().build(); httpEntity.setURI(URI.create(open_api_url_prefix + uri)); httpEntity.setEntity(new StringEntity(JSONObject.toJSONString(data), ContentType.APPLICATION_JSON)); response = client.execute(httpEntity); if (response != null && response.getStatusLine().getStatusCode() == 200) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = bufferedReader.readLine()) != null) { result.append(line); } bufferedReader.close(); return JSONObject.parseObject(result.toString()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } client.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }这段代码能简写吗
可以简化这段代码。下面是简化后的代码:
```java
public static JSONObject publishDmpCustomAudience() {
String access_token = "xxx";
final Long advertiser_id = 0L;
// 请求地址
String open_api_url_prefix = "https://ad.oceanengine.com/open_api/2/";
String uri = "dmp/custom_audience/publish/";
// 请求参数
Map<String, Object> data = new HashMap<>();
data.put("advertiser_id", advertiser_id);
data.put("custom_audience_id", 3);
// 构造请求
HttpPost httpEntity = new HttpPost(open_api_url_prefix + uri);
httpEntity.setHeader("Access-Token", access_token);
httpEntity.setEntity(new StringEntity(JSONObject.toJSONString(data), ContentType.APPLICATION_JSON));
try (CloseableHttpClient client = HttpClientBuilder.create().build();
CloseableHttpResponse response = client.execute(httpEntity)) {
if (response != null && response.getStatusLine().getStatusCode() == 200) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
bufferedReader.close();
return JSONObject.parseObject(result.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
```
这里的改动包括:
- 使用泛型指定 `data` 的类型为 `Map<String, Object>`;
- 使用 try-with-resources 简化关闭资源的代码;
- 使用 `StringBuilder` 替代 `StringBuffer`,因为前者是线程安全的,后者不是;
- 移除了无用的异常捕获。
这样代码会更简洁和易读。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)