與B項目進行交互,由于B項目一些原因,請求處理緩慢.A項目等待返回結果需要很久...現(xiàn)只需發(fā)送數(shù)據(jù)無需判斷發(fā)送成功失敗與否,求教如何操作???以下是發(fā)送get請求的代碼
public static String doGet(String HTTP_URL, Object object) {
BufferedReader reader = null;
String result = null;
StringBuffer httpUrl = new StringBuffer(HTTP_URL);
StringBuffer sbf = new StringBuffer();
try {
System.out.println(httpUrl.toString());
URL url = new URL(httpUrl.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
// 請求方式設置 POST
connection.setRequestMethod("GET");
// 設置維持長連接
connection.setRequestProperty("Connection", "Keep-Alive");
// 設置文件字符集:
connection.setRequestProperty("Charset", "UTF-8");
// 開始連接請求
connection.connect();
OutputStream out = connection.getOutputStream();
out.write((object.toString()).getBytes());
out.flush();
out.close();
if (connection.getResponseCode() == 200) {
System.out.println("連接成功,傳送數(shù)據(jù)...");
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
if (result.equals("1")) {
return "1";
} else if(result.equals("0")) {
return "0";
} else {
return result;
}
} else {
System.out.println("連接失敗,錯誤代碼:"+connection.getResponseCode());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
2 回答

侃侃無極
TA貢獻2051條經(jīng)驗 獲得超10個贊
public static String doGet(String HTTP_URL, Object object) {
BufferedReader reader = null;
String result = null;
StringBuffer httpUrl = new StringBuffer(HTTP_URL);
StringBuffer sbf = new StringBuffer();
try {
System.out.println(httpUrl.toString());
URL url = new URL(httpUrl.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
// 請求方式設置 POST
connection.setRequestMethod("POST");
// 設置文件字符集:
connection.setRequestProperty("Charset", "UTF-8");
// 開始連接請求
connection.connect();
OutputStream out = connection.getOutputStream();
out.write((object.toString()).getBytes());
out.flush();
out.close();
if (connection.getResponseCode() == 200) {
System.out.println("連接成功,傳送數(shù)據(jù)...");
} else {
System.out.println("連接失敗,錯誤代碼:"+connection.getResponseCode());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
添加回答
舉報
0/150
提交
取消