http发送http报文
- package com.lkt.jdk;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- public class GetHttp {
- public static String getWebContent()throws IOException{
- //创建URL,协议、ip、端口、访问的页面
- URL url=new URL("HTTP","192.168.22.209",7001,"/Request_USER_LOGIN");
- //创建HttpURLConnection 对象
- HttpURLConnection conn=(HttpURLConnection)url.openConnection();
- //设置请求方式POST
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "application/xml");
- conn.setRequestProperty("Connection", "Keep-Alive");
- //请求的数据
- String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?> "+
- "<HTTP_XML EventType=\"Request_USER_LOGIN\" TargetSyscode=\"\" RequestId=\"\" TransactionId=\"\">"+
- " <Item Name=\"admin\" Password=\"670b14728ad9902aecba32e22fa4f6bd\" UserIp=\"\" Port=\"\" BusinessSysCode=\"\"/>"+
- "</HTTP_XML>";
- //数据长度
- conn.setRequestProperty("Content-Length",xml.length()+"");
- //设置输入和输出流
- conn.setDoOutput(true);
- conn.setDoInput(true);
- //将参数输出到连接
- //使用这种方式,我在本地执行没有问题,在weblogic下出现过错误
- //java.net.ProtocolException: Did not meet stated content length of OutputStream: you wrote 0 bytes and I was expecting you to write exactly 241 bytes!!!
- //我使用第二中方法就可以了
- /*方法一
- conn.getOutputStream().write(xml.getBytes("UTF-8"));
- conn.getOutputStream().flush();
- conn.getOutputStream().close();
- */
- /*
- * 方法二 */
- DataOutputStream dos=new DataOutputStream(conn.getOutputStream());
- dos.writeBytes(xml);
- dos.flush();
- dos.close();
- //得到HTTP响应码
- int code=conn.getResponseCode();
- InputStream is=conn.getInputStream();
- //获取返回报文
- BufferedReader bis=new BufferedReader(new InputStreamReader(is));
- StringBuffer sb=new StringBuffer();
- String temp="";
- while((temp=bis.readLine())!=null){
- sb.append(temp+"\n");
- }
- return null;
- }
- public static void main(String[] args)throws Exception {
- getWebContent();
- }
- }
posted on 2015-06-10 01:14 zhaoshuzhan 阅读(451) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?