java模拟http的简单介绍

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

今天给各位分享java模拟http的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

怎么用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=xxxpwd=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数据包,该如何处理

左边是发送,右边是接收,

同时左右两边都能实现发送与接收

怎样用JAVA实现模拟HTTP请求,得到服务器的响应时间等参数

a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4n1Ddn1ubuHI-Pjc1uycz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnW0LPHfzPHm4PHczPH0YrjTsr0" target="_blank" class="baidu-highlight"java.net/a.*;

public class HttpDemo{

    public static void main(String[] args)throws Exception{

        URL url = new URL('地址');

        HttpURLConnection http = (HttpURLConnection)url.openConnection();

        //获取网页的源码

        BufferedReader br = new BufferedReader(new InputStreamReader(http.getInputStream()));

        String line = "";

        while((line=br.readLine())!=null){

            System.out.println(line);

        }

        br.close();

        //获取参数:

        String value = getRequestProperty(String key);

    }

}

linux下java 模拟HTTP请求和window下有上面区别吗

没有区别吧,所有的浏览器都是遵循http协议来发送请求的,有固定的报文头;服务端的返回信息也遵循该协议。

反正我用java写的web应用部署后,linux和window下用浏览器去访问都是一个效果。

java模拟http请求指定url

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet("");

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接口?

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模拟http和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

发布于 2023-04-02 23:04:50
收藏
分享
海报
36
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了