httpUrlConnection如何做到连接超时重复发送请求3次然后判定失败呢

小崔侃大山 2020-08-03 11:19:18
httpUrlConnection如何做到连接超时重复发送请求3次然后判定失败呢,捕获了超时异常,在catch中如何处理呢让其重复发送规定次数呢
...全文
6655 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
guava-retrying
evanweng 2020-08-31
  • 打赏
  • 举报
回复
public Object req(int retryCount) { try{ // 网络请求 } catch(Exception e) { // 或者有指定某些异常 if (retryCount> 0) { return req(--retryCount); } else { throw e; } } }
GG哥 2020-08-28
  • 打赏
  • 举报
回复
引用 6 楼 tianfang 的回复:
读取双色球api的代码,接口现在已经不可用。练手代码,只是可用的水平

	public static String getDCBallHistory() {

		InputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader reader = null;
		StringBuffer resultBuffer = new StringBuffer();
		String tempLine = null;

		boolean flag = false;
		int count = 0;

//		System.setProperty("sun.net.client.defaultConnectTimeout", "2500");
//		System.setProperty("sun.net.client.defaultReadTimeout", "3500");

		while (count < 3) {

			try {

				URL apiUrl = new URL("http://f.apiplus.net/ssq-50.json");
				
				HttpURLConnection httpURLConnection = (HttpURLConnection) apiUrl.openConnection();
				
				httpURLConnection.setConnectTimeout(5000);
				httpURLConnection.setReadTimeout(5000); 
				httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
				inputStream = httpURLConnection.getInputStream();
				inputStreamReader = new InputStreamReader(inputStream);
				reader = new BufferedReader(inputStreamReader);
				if (httpURLConnection.getResponseCode() >= 300) {
					throw new Exception(
							"HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
				}

				while ((tempLine = reader.readLine()) != null) {
					resultBuffer.append(tempLine);
				}

			} catch (Exception e) {
				// e.printStackTrace();
				flag = true;
				System.out.println("direct get " + count + " times");
			} finally {

				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

				if (inputStreamReader != null) {
					try {
						inputStreamReader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
			if (flag) {
				count++;
			} else {
				break;
			}

		}

		return resultBuffer.toString();
	}


是我想的有问题?如果第1次请求失败 flag=true了,再次循环不重置为false,即使请求成功了也不会走break
slh__0 2020-08-04
  • 打赏
  • 举报
回复
请问怎么获取一个积分
tianfang 2020-08-04
  • 打赏
  • 举报
回复
读取双色球api的代码,接口现在已经不可用。练手代码,只是可用的水平

	public static String getDCBallHistory() {

		InputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader reader = null;
		StringBuffer resultBuffer = new StringBuffer();
		String tempLine = null;

		boolean flag = false;
		int count = 0;

//		System.setProperty("sun.net.client.defaultConnectTimeout", "2500");
//		System.setProperty("sun.net.client.defaultReadTimeout", "3500");

		while (count < 3) {

			try {

				URL apiUrl = new URL("http://f.apiplus.net/ssq-50.json");
				
				HttpURLConnection httpURLConnection = (HttpURLConnection) apiUrl.openConnection();
				
				httpURLConnection.setConnectTimeout(5000);
				httpURLConnection.setReadTimeout(5000); 
				httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
				inputStream = httpURLConnection.getInputStream();
				inputStreamReader = new InputStreamReader(inputStream);
				reader = new BufferedReader(inputStreamReader);
				if (httpURLConnection.getResponseCode() >= 300) {
					throw new Exception(
							"HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
				}

				while ((tempLine = reader.readLine()) != null) {
					resultBuffer.append(tempLine);
				}

			} catch (Exception e) {
				// e.printStackTrace();
				flag = true;
				System.out.println("direct get " + count + " times");
			} finally {

				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

				if (inputStreamReader != null) {
					try {
						inputStreamReader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
			if (flag) {
				count++;
			} else {
				break;
			}

		}

		return resultBuffer.toString();
	}


qybao 2020-08-04
  • 打赏
  • 举报
回复
for或while循环里try catch,如果没有异常,直接break循环;如果异常,在catch里判断是否是超时异常,是的话累加try次数,如果try次数大于3次,则break循环,否则重新循环try catch
小崔侃大山 2020-08-04
  • 打赏
  • 举报
回复
这个重试参数定义在哪,会不会有线程安全问题呢?
小崔侃大山 2020-08-04
  • 打赏
  • 举报
回复
这个重试参数定义在哪,会不会有线程安全问题呢?
evanweng 2020-08-04
  • 打赏
  • 举报
回复
递归,方法加一个重试次数参数
小崔侃大山 2020-08-04
  • 打赏
  • 举报
回复
谢谢您的答复,其实我是想集思广益看看有什么其他的实现方式

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧