導航:首頁 > 編程語言 > 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請求相關的資料

熱點內容
現代鋼琴教程pdf 瀏覽:23
客戶端框架源碼 瀏覽:206
python自動辦公能幹嘛 瀏覽:873
程序員追愛 瀏覽:252
程序員邏輯故事 瀏覽:768
加密icsot23i2c 瀏覽:713
你們有什麼好的解壓軟體 瀏覽:607
常州空氣壓縮機廠家 瀏覽:241
安卓如何關閉app內彈出的更新提示 瀏覽:409
e4a寫的app怎麼裝蘋果手機 瀏覽:201
海立壓縮機海信系 瀏覽:210
社保如何在app上合並 瀏覽:220
小米加密照片後綴 瀏覽:236
我的世界網易手機怎麼創伺服器 瀏覽:978
載入單頁源碼 瀏覽:930
阿里雲伺服器seo 瀏覽:777
海洋斗什麼時候上線安卓 瀏覽:86
中行app如何查每日匯款限額 瀏覽:840
輸入伺服器sn是什麼意思 瀏覽:725
sha1演算法java 瀏覽:90