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