Http请求重复发送

============问题描述============


以下是android发送Http请求的代码,但是经常出现:服务器响应很久, android端出现ReadTimeout, android端又会再发一次同样的请求,导致数据重复。有人说是jdk1.4.2的问题,但我用的是1.6.0。请大神们帮忙
        
 
 URL url = null;

		HttpURLConnection conn = null;

		String result = "";



		try {

			String sParam = "&m=" + methodName;

			if (vector != null) {

				for (int i = 0; i < vector.size(); i++) {

					sParam += "&p" + String.valueOf(i) + "=" + vector.elementAt(i).toString();

				}

			}

			byte[] data = sParam.getBytes();

			

			url = new URL(realURL.replace("xx.mobile", methodName+".mobile"));



			conn = (HttpURLConnection) url.openConnection();



			conn.setConnectTimeout(CONNECT_TIMEOUT);



			conn.setReadTimeout(READ_TIMEOUT);



			conn.setDoInput(true);



			conn.setDoOutput(true);



			conn.setRequestMethod(METHOD_POST);



			conn.setRequestProperty("Connection", "Keep-Alive");



			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");



			conn.setRequestProperty("Content-Length", String.valueOf(data.length));



			conn.setRequestProperty("Charset", "utf-8");

			

			conn.setUseCaches(false);



			OutputStream outStream = conn.getOutputStream();



			outStream.write(data);



			outStream.flush();



			outStream.close();



			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) // ResponseCode:200

			{

				InputStream inStream = conn.getInputStream();

				//int contentLen = conn.getContentLength();

				result = readInputStream(inStream,1024);



				inStream.close();

			} else {

				result = "500"; //server error

			}

		} catch (ConnectTimeoutException e) {

			// TODO: handle exception

			CommonData.Log("ConnectTimeoutException");

			throw new MyException(HTTP_RESULT_CONNECT_TIMEOUT);

		} catch (SocketTimeoutException e) {

			// TODO: handle exception

			CommonData.Log("SocketTimeoutException");

			throw new MyException(HTTP_RESULT_READ_TIMEOUT);

		} catch (Exception e) {

			// TODO: handle exception

			CommonData.Log(e.getLocalizedMessage());

			throw new MyException(HTTP_RESULT_EXCEPTION);

		} finally {

			conn.disconnect();

		}

============解决方案1============


尝试用 httpclient 吧  

 看不出什么问题,属于疑难杂症吧

============解决方案2============


底层的实现中 如果发现http请求超时  有重发机制

============解决方案3============


引用 16 楼 Roy_se7en 的回复:
Quote: 引用 15 楼 ysh512 的回复:


Quote: 引用 13 楼 Roy_se7en 的回复:

Quote: 引用 11 楼 ysh512 的回复:

底层的实现中 如果发现http请求超时  有重发机制


请问,这个有资料可查吗?如何能禁用呢?



以前在一个人的blog中看到的,现在找不到了


麻烦帮忙找下,万分感谢。


原来那个找不到了 你看看这个 意思类似
http://blog.csdn.net/hanqunfeng/article/details/4510338

============解决方案4============


http://developer.nokia.com/community/wiki/Using_HTTP_and_HTTPS_in_Java_ME

这个你看下  

还有你的j2me 中的相关代码 也请po出来, 前面的是 android中的代码。

============解决方案5============


1:
http://bugs.java.com/view_bug.do?bug_id=6672144

这里面描述的 bug 和你说的很像。

它的解决方案是设置系统属性。
From JDK6 you can set the system property, sun.net.http.retryPost, to false to prevent the HTTP client from silently resending the POST request. 
  java -Dsun.net.http.retryPost=false ...


2:
http://developer.nokia.com/community/discussion/showthread.php/233689-HTTP-Connection-sending-request-twice-after-30-sec

这个完全是你说的案例。它的建议是后台参与进来 




============解决方案6============


引用 32 楼 Roy_se7en 的回复:
Quote: 引用 31 楼 foolsheep 的回复:

连接超时和读取超时时间会不会不一致?


这2者不一致有关系吗?


如果连接还没有超时,而读取时间超时的话,HttpUrlConnection就会再重新发送请求。所以,要解决这个重发问题,可以试一下,把读取超时的时间设置得比连接超时稍长一点,因为有时候,jdk计算的时间会有点偏差,这样可以保证在连接超时之前不会读取超时

转载于:https://www.cnblogs.com/liangxieliang56/p/4089472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用axios发送请求时,我们可以使用axios的CancelToken来取消重复请求。 首先,我们需要创建一个axios实例,然后在拦截器中检查是否有相同的请求正在进行中,如果有,则取消重复请求。 以下是一个示例代码: ```javascript import axios from 'axios'; const instance = axios.create({ baseURL: 'https://example.com/api', }); let pending = []; // 存储所有正在进行的请求 const cancelToken = axios.CancelToken; const removePending = (config) => { for (let i = 0; i < pending.length; i++) { if (pending[i].url === config.url + '&' + config.method) { // 取消重复请求 pending[i].cancel(); pending.splice(i, 1); } } }; instance.interceptors.request.use( (config) => { removePending(config); config.cancelToken = new cancelToken((c) => { pending.push({ url: config.url + '&' + config.method, cancel: c }); }); return config; }, (error) => { return Promise.reject(error); } ); instance.interceptors.response.use( (response) => { removePending(response.config); return response; }, (error) => { return Promise.reject(error); } ); export default instance; ``` 在上面的代码中,我们创建了一个名为“pending”的数组来存储所有正在进行的请求。在请求拦截器中,我们使用axios的CancelToken来创建一个取消令牌,并将其添加到请求配置中。同时,我们还将请求的URL和HTTP方法作为键值对存储在“pending”数组中,以便我们可以在拦截器中检查是否存在相同的请求正在进行中。如果有相同的请求正在进行中,则取消新请求。在响应拦截器中,我们从“pending”数组中删除已完成的请求。 这样,我们就可以避免发送重复请求了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值