導航:首頁 > 編程語言 > java模擬http請求

java模擬http請求

發布時間:2022-08-07 02:43:42

java用HttpURLConnection(GET) 模擬http請求 如何設置參數的編碼

new URL(url); 的時候 參數url就可以像js里一樣 拼參數啊
至於編碼 那就是url這個字元串 可以直接轉編碼啊

❷ 怎樣用JAVA實現模擬HTTP請求,得到伺服器的響應時間等參數

問題簡化一下:對一個ip,一個線程請求100次。該次請求的響應時間為調用httpClient前的響應時間減去接收到httpClient響應的時間。注意,本次請求是否有效要判斷。平均響應時間和最大響應時間只不過是響應時間的統計而已,可以用資料庫來做。

就是說資料庫記錄每次測試請求的響應時間,成功與否。統計數據最後出來。
只所以用多線程,是因為單線程順序請求100次,不能模擬伺服器真正的情況。

❸ 用java的Socket來模擬Http協議的發送與接收。

看下netty不就行了,裡面有現成的http的codec,不用自己搞...

❹ 怎樣java向伺服器發送http請求

你好,java有一個組件,httpclient,這個jar包,可以模擬http客戶端,向服務端發送http請求,而且現在大部分都用的是這個。

❺ 用JAVA實現模擬HTTP請求,得到伺服器響應時間等參數

之前看過一下。。用純java控制台實現http請求的。。 好像只是1.6 update * 的產品。

❻ java模擬http請求指定url

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet("http://localhost/");

HttpResponse response = httpclient.execute(httpget);

HttpEntity entity = response.getEntity();

if (entity != null) {

InputStream instream = entity.getContent();

int l;

byte[] tmp = new byte[2048];

while ((l = instream.read(tmp)) != -1) {

}

}
大體上就是這樣了。

❼ 怎麼用java模擬http請求

/*
* 得到返回的內容
*/
public static String getResult(String urlStr, String content) {
URL url = null;
HttpURLConnection connection = null;

try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.connect();

DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), "utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
追問:
沒注釋嗎?
追答:
/*
* 得到返回的內容
*/
public static String getResult(String urlStr, String content) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();//新建連接實例
connection.setDoOutput(true);//是否打開輸出流 true|false
connection.setDoInput(true);//是否打開輸入流true|false
connection.setRequestMethod("POST");//提交方法POST|GET
connection.setUseCaches(false);//是否緩存true|false
connection.connect();//打開連接埠
DataOutputStream out = new DataOutputStream(connection.getOutputStream());//打開輸出流往對端伺服器寫數據
out.writeBytes(content);//寫數據,也就是提交你的表單 name=xxx&pwd=xxx
out.flush();//刷新
out.close();//關閉輸出流
BufferedReader reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), "utf-8"));//往對端寫完數據 對端伺服器返回數據 ,以BufferedReader流來讀取
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();//關閉連接
}
}
return null;
}

閱讀全文

與java模擬http請求相關的資料

熱點內容
win7掃描pdf 瀏覽:43
android藍牙開發文檔 瀏覽:350
java垃圾回收原理 瀏覽:875
怎麼把電腦文件夾復制到D盤 瀏覽:176
linuxtcp請求 瀏覽:260
pdcp加密范圍 瀏覽:669
6p照片加密 瀏覽:363
安卓桌面快捷鍵怎麼刪除 瀏覽:793
成都麻將2元加點演算法 瀏覽:664
股票每天高點低點演算法 瀏覽:20
使用路由器設置伺服器地址 瀏覽:274
matlab求逆矩陣的演算法 瀏覽:153
javaweburl 瀏覽:606
dota命令大全單機 瀏覽:711
android44屏蔽home鍵 瀏覽:886
五金程序員編程多少錢一個月 瀏覽:177
雲伺服器違規對外攻擊 瀏覽:188
phprips 瀏覽:7
python怎麼單干 瀏覽:385
創維安卓投屏為什麼沒聲音 瀏覽:750