與B項目進行交互,由于B項目一些原因,請求處理緩慢.A項目等待返回結果需要很久...現只需發送數據無需判斷發送成功失敗與否,求教如何操作???以下是發送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);//請求方式設置POSTconnection.setRequestMethod("GET");//設置維持長連接connection.setRequestProperty("Connection","Keep-Alive");//設置文件字符集: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("連接成功,傳送數據...");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("連接失敗,錯誤代碼:"+connection.getResponseCode());}}catch(Exceptione){e.printStackTrace();}returnnull;}
java發送http請求,無需等待返回結果
慕田峪9158850
2019-05-24 11:09:58