关于javahttp测试的信息
本篇文章给大家谈谈javahttp测试,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java发送http请求500异常
- 2、怎样用JAVA实现模拟HTTP请求,得到服务器的响应时间等参数
- 3、java如何创建一个简单的http接口?
- 4、java编写接口测试DEMO
- 5、怎样用JAVA计算百分比,比如我做了一个HTTP测试程序,需要在一段时间内计算连接成功和失败的百分比,怎么
java发送http请求500异常
是你请求的那个url服务出问题了 正常返回200状态码 但是服务返回500,请求的服务发生异常。去看看调用服务的日志
怎样用JAVA实现模拟HTTP请求,得到服务器的响应时间等参数
问题简化一下:对一个ip,一个线程请求100次。该次请求的响应时间为调用httpClient前的响应时间减去接收到httpClient响应的时间。注意,本次请求是否有效要判断。平均响应时间和最大响应时间只不过是响应时间的统计而已,可以用数据库来做。
就是说数据库记录每次测试请求的响应时间,成功与否。统计数据最后出来。
只所以用多线程,是因为单线程顺序请求100次,不能模拟服务器真正的情况。
java如何创建一个简单的http接口?
1.修改web.xml文件
!-- 模拟HTTP的调用,写的一个http接口 -- servlet servlet-nameTestHTTPServer/servlet-name servlet-classcom.atoz.http.SmsHTTPServer/servlet-class /servlet servlet-mapping servlet-nameTestHTTPServer/servlet-name url-pattern/httpServer/url-pattern /servlet-mapping
2.新建SmsHTTPServer.java文件
package com.atoz.http;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.atoz.action.order.SendSMSAction; import com.atoz.util.SpringContextUtil;
public class SmsHTTPServer extends HttpServlet { private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); String content = request.getParameter("content"); //String content = new String(request.getParameter("content").getBytes("iso-8859-1"), "utf-8"); String mobiles = request.getParameter("mobiles"); String businesscode = request.getParameter("businesscode"); String businesstype = request.getParameter("businesstype"); if (content == null || "".equals(content) || content.length() = 0) { System.out.println("http call failed,参数content不能为空,程序退出"); } else if (mobiles == null || "".equals(mobiles) || mobiles.length() = 0) { System.out.println("http call failed,参数mobiles不能为空,程序退出"); } else { /*SendSMSServiceImpl send = new SendSMSServiceImpl();*/ SendSMSAction sendSms = (SendSMSAction) SpringContextUtil.getBean("sendSMS"); sendSms.sendSms(content, mobiles, businesscode, businesstype); System.out.println("---http call success---"); } out.close(); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
3.调用http接口
String content = "测试"; content = URLEncoder.encode(content, "utf-8"); String url = "" + content + "mobiles=15301895007"; URL httpTest; try { httpTest = new URL(url); BufferedReader in; try { in = new BufferedReader(new InputStreamReader( httpTest.openStream())); String inputLine = null; String resultMsg = null; //得到返回信息的xml字符串 while ((inputLine = in.readLine()) != null) if(resultMsg != null){ resultMsg += inputLine; }else { resultMsg = inputLine; } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
打字不易,望采纳,谢谢
java编写接口测试DEMO
---随便在网上一搜一大把
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLAccess {
public static void main(String[] args) {
try {
test();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void test() throws IOException {
URL url = new URL("");
System.out.println("==================以下为网站内容==================");
URLConnection urlcon = url.openConnection();
int i = urlcon.getContentLength();
if (i 0) {
InputStream is = urlcon.getInputStream();
int a;
while ((a = is.read()) != -1) {
System.out.print((char) a);
}
is.close();
} else {
System.out.println("响应内容为空...");
}
}
}
怎样用JAVA计算百分比,比如我做了一个HTTP测试程序,需要在一段时间内计算连接成功和失败的百分比,怎么
public double fun(int a,int b){
//a表示成功次数,b表示失败次数,c表示成功百分比,d表示失败百分比
double c=(a/(a+b))*100;
double d=(b/(a+b))*100;
//返回成功百分比
return c;
//返回失败百分比
//return d;
}
得到的是一个double类型的数据,具体保留几位小数啊什么的自己处理一下就可以了。
javahttp测试的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javahttp测试的信息别忘了在本站进行查找喔。