- 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+" ");
- }
- return null;
- }
- public static void main(String[] args)throws Exception {
- getWebContent();
- }
- }