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

熱點內容
電視看籃球app哪個好 瀏覽:47
高中畢業當程序員 瀏覽:245
php標簽屬性大全 瀏覽:897
遠程訪問伺服器ip地址 瀏覽:312
程序員吃雞蛋炒菜 瀏覽:173
在哪裡看俄羅斯電視劇app 瀏覽:308
怎麼找資料庫伺服器地址 瀏覽:487
伺服器調試怎麼翻譯 瀏覽:921
php如何處理ajax請求 瀏覽:211
php數組下標存在 瀏覽:707
php獲取ip歸屬地 瀏覽:175
撩女程序員怎麼辦 瀏覽:508
百度伺服器做什麼 瀏覽:193
打開軟體加速伺服器有什麼危害 瀏覽:87
php去除數組下標 瀏覽:794
ipad的app內容哪裡看 瀏覽:284
遇見空間app在哪裡 瀏覽:547
用命令對一個文件內容進行統計 瀏覽:317
華為交換機配置命令縮寫 瀏覽:344
鏈接伺服器出現問題怎麼處理 瀏覽:834