请求超时处理

package com.it;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpConnProcessThread implements Runnable {

public boolean isStop = false;

public boolean readOK = false;

private static HttpURLConnection reqConnection = null;

public Thread readingThread;

private int readLen;

private String msg = null;

private String reqMethod;

private byte[] data;

/**
* ReadThread constructor comment.
*/
public HttpConnProcessThread(HttpURLConnection reqConnection, String msg, String reqMethod ) {
super();
this.reqConnection = reqConnection;
this.msg = msg;
this.reqMethod = reqMethod;
}

public void run() {

InputStream input = null;
OutputStream output = null;

try{
//reqConnection.connect();
output = reqConnection.getOutputStream();
if ("post".equalsIgnoreCase(reqMethod) && msg != null && msg.length() >0)
{
output.write(msg.getBytes());
output.close();
output = null;
}

// 处理HTTP响应的返回状态信息
int responseCode = reqConnection.getResponseCode();// 响应的代码if( responseCode != 200 )
System.out.println("connect failed! responseCode = " + responseCode + " msg=" + reqConnection.getResponseMessage());

input = reqConnection.getInputStream();

int len;
byte[] buf = new byte[2048];
readLen = 0;
// 读取inputStream
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while (!isStop)
{
len = input.read(buf);
if (len <= 0)
{
this.readOK = true;
input.close();
data=outStream.toByteArray();
break;
}
outStream.write(buf, 0, len);
readLen += len;
}
}
catch( IOException ie)
{}
catch(Exception e)
{}
finally
{
try{
reqConnection.disconnect();
if( input != null )
input.close();
if( output != null )
output.close();

//唤醒线程,跳出等待
wakeUp();
}catch(Exception e)
{

}
}
}

public String getMessage(){
if (!readOK) //超时
{
return "";
}

if (readLen <= 0) {
return "";
}
return new String(data, 0, readLen);
}

public void startUp() {
this.readingThread = new Thread(this);
readingThread.start();
}


//唤醒线程,不再等待
private synchronized void wakeUp() {
notifyAll();
}

public synchronized void waitForData(int timeout)
{
try {
//指定超时时间,等待connection响应
wait(timeout);
}
catch (Exception e)
{
}

if (!readOK)
{
isStop = true;
try{
//中断线程
if( readingThread.isAlive() )
readingThread.interrupt();
}catch(Exception e)
{

}
}
}

public static void main(String[] args) throws IOException{
String msg="";
URL reqUrl = new URL("http://127.0.0.1:8080/");

// 建立URLConnection连接
reqConnection = (HttpURLConnection) reqUrl.openConnection();
HttpConnProcessThread rec = new HttpConnProcessThread(reqConnection, msg, "post" );
// 如果顺利连接到并读完数据,则跳出等待,否则等待超时
rec.startUp();
rec.waitForData(2000);

String retMessage = rec.getMessage();
}
}

转载于:https://www.cnblogs.com/jareny/p/10854644.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于axios请求超时处理,可以通过以下几个步骤来实现: 1. 创建一个axios实例,并在实例设置请求超时时间timeout。可以使用axios的defaults属性来设置全局默认值。例如,可以将timeout设置为6000毫秒,即6秒:axios.defaults.timeout = 6000; 2. 当请求超时时,可以通过设置axios的retry属性来进行重新请求。retry属性表示请求失败后的重试次数。例如,可以将retry设置为3次:axios.defaults.retry = 3; 3. 为了避免频繁的重试请求,可以设置一个请求间隔retryDelay。retryDelay表示两次请求之间的时间间隔。例如,可以将retryDelay设置为1000毫秒,即1秒:axios.defaults.retryDelay = 1000; 通过以上步骤,就可以实现axios请求超时处理。当请求超时时,axios会自动进行重新请求,直到达到重试次数或请求成功为止。这样可以提高请求的稳定性和可靠性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [axios请求超时,设置重新请求的完美解决方法](https://blog.csdn.net/weixin_34228662/article/details/91377463)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue axios请求超时的正确处理方法](https://download.csdn.net/download/weixin_38709466/12761674)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [axios请求失败、请求超时重新发送请求](https://blog.csdn.net/weixin_47437528/article/details/128035261)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值