❶ 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;
}