第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

java發(fā)送http請求,無需等待返回結(jié)果

java發(fā)送http請求,無需等待返回結(jié)果

慕田峪9158850 2019-05-24 11:09:58
與B項(xiàng)目進(jìn)行交互,由于B項(xiàng)目一些原因,請求處理緩慢.A項(xiàng)目等待返回結(jié)果需要很久...現(xiàn)只需發(fā)送數(shù)據(jù)無需判斷發(fā)送成功失敗與否,求教如何操作???以下是發(fā)送get請求的代碼publicstaticStringdoGet(StringHTTP_URL,Objectobject){BufferedReaderreader=null;Stringresult=null;StringBufferhttpUrl=newStringBuffer(HTTP_URL);StringBuffersbf=newStringBuffer();try{System.out.println(httpUrl.toString());URLurl=newURL(httpUrl.toString());HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();connection.setDoOutput(true);connection.setDoInput(true);//請求方式設(shè)置POSTconnection.setRequestMethod("GET");//設(shè)置維持長連接connection.setRequestProperty("Connection","Keep-Alive");//設(shè)置文件字符集:connection.setRequestProperty("Charset","UTF-8");//開始連接請求connection.connect();OutputStreamout=connection.getOutputStream();out.write((object.toString()).getBytes());out.flush();out.close();if(connection.getResponseCode()==200){System.out.println("連接成功,傳送數(shù)據(jù)...");InputStreamis=connection.getInputStream();reader=newBufferedReader(newInputStreamReader(is,"UTF-8"));StringstrRead=null;while((strRead=reader.readLine())!=null){sbf.append(strRead);sbf.append("\r\n");}reader.close();result=sbf.toString();if(result.equals("1")){return"1";}elseif(result.equals("0")){return"0";}else{returnresult;}}else{System.out.println("連接失敗,錯(cuò)誤代碼:"+connection.getResponseCode());}}catch(Exceptione){e.printStackTrace();}returnnull;}
查看完整描述

2 回答

?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

HttpURLConnection有個(gè)setReadTimeout的方法可以實(shí)現(xiàn)你的需求。
publicstaticStringdoGet(StringHTTP_URL,Objectobject){
BufferedReaderreader=null;
Stringresult=null;
StringBufferhttpUrl=newStringBuffer(HTTP_URL);
StringBuffersbf=newStringBuffer();
HttpURLConnectionconnection=null;
try{
System.out.println(httpUrl.toString());
URLurl=newURL(httpUrl.toString());
connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
//請求方式設(shè)置POST
connection.setRequestMethod("GET");
//設(shè)置維持長連接
connection.setRequestProperty("Connection","Keep-Alive");
//設(shè)置文件字符集:
connection.setRequestProperty("Charset","UTF-8");
//根據(jù)需求設(shè)置讀超時(shí)的時(shí)間
connection.setReadTimeout(50);
//開始連接請求
connection.connect();
OutputStreamout=connection.getOutputStream();
out.write((object.toString()).getBytes());
out.flush();
out.close();
if(connection.getResponseCode()==200){
System.out.println("連接成功,傳送數(shù)據(jù)...");
InputStreamis=connection.getInputStream();
reader=newBufferedReader(newInputStreamReader(is,"UTF-8"));
StringstrRead=null;
while((strRead=reader.readLine())!=null){
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result=sbf.toString();
if(result.equals("1")){
return"1";
}elseif(result.equals("0")){
return"0";
}else{
returnresult;
}
}else{
System.out.println("連接失敗,錯(cuò)誤代碼:"+connection.getResponseCode());
}
}catch(Exceptione){
if(einstanceofSocketTimeoutException){
if("Readtimedout".equals(e.getMessage())&&connection!=null&&connection.getDoOutput()){
//TODO只請求不需要響應(yīng)
returnnull;
}
}
e.printStackTrace();
}
returnnull;
}
                            
查看完整回答
反對 回復(fù) 2019-05-24
  • 2 回答
  • 0 關(guān)注
  • 3884 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號