瑶瑶科技 请求样例. (保留一周)
package test; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; public class test1 { public static void main(String[] args) throws Exception{ String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ "<PACKET type=\"REQUEST\" version=\"1.0\">"+ "<HEAD>"+ "<REQUEST_TYPE>0300-05</REQUEST_TYPE>"+ "<SYSNAME>电销</SYSNAME>"+ "<SYSCODE>01</SYSCODE>"+ "<USERCDE>100009999</USERCDE>"+ "<DPTCDE>33</DPTCDE>"+ "<TYPE>I</TYPE>"+ "<DATA>A</DATA>"+ "<SERVICE_NO></SERVICE_NO>"+ "<SERVICENAME></SERVICENAME>"+ "<USERID></USERID>"+ "</HEAD>"+ "<BODY>"+ "<VHL>"+ "<VEHICLE_ID></VEHICLE_ID>"+ "<VEHICLE_NAME>宝马 740</VEHICLE_NAME>"+ "<VIN></VIN>"+ "</VHL>"+ "</BODY>"+ "</PACKET>"; //把XML的数据转成字符串 System.out.println(xml); String returnData=sendRequestData(xml, "http://esbtest.zsins.com:8866/0203000003/VhlModelSearch/V1","UTF-8","UTF-8", 3000,3000); } public static String sendRequestData(String requestData, String urlStr,String sendEncoding,String recvEncoding, int connectionTimeout,int readTimeout) throws IOException{ URL url = null; HttpURLConnection conn = null; ByteArrayOutputStream byteOut = null; BufferedReader readInfo = null; StringBuffer strBuilder=new StringBuffer(); OutputStream out = null; try { System.out.println("请求时间:【"+new Date()+"】"); System.out.println("请求地址:【"+urlStr+"】"); System.out.println("请求报文:【"+requestData+"】"); url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("SOAPAction", "\"\""); conn.setRequestProperty("Accept", "application/soap+xml, application/dime, multipart/related, text/*"); conn.setRequestProperty("Cache-Control", "no-cache"); conn.setUseCaches(false); //忽略缓存 conn.setDoOutput(true); //使用 URL 连接进行输出 conn.setDoInput(true); //使用 URL 连接进行输入 conn.setConnectTimeout(connectionTimeout);//链接超时 conn.setReadTimeout(readTimeout);//读取超时 conn.connect();//建立链接 byteOut = new ByteArrayOutputStream(); byteOut.write(requestData.getBytes(sendEncoding));//以指定编码发送,如果有乱码,修改之 byte[] buf = byteOut.toByteArray(); out = conn.getOutputStream(); out.write(buf); out.flush(); if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {//正确返回 readInfo = new BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),recvEncoding));//以指定编码读取返回信息,如果有乱码,修改之 String line = null; while ((line = readInfo.readLine()) != null) { strBuilder.append(line); } } else {//没有正确返回 readInfo = new BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),recvEncoding));//以指定编码读取返回信息,如果有乱码,修改之 System.out.println("出现异常,返回报文:【"+readInfo+"】"); throw new IOException("url请求出现问题,返回编码:" + conn.getResponseCode()); } System.out.println("返回时间:【"+new Date()+"】"); System.out.println("返回报文:【"+strBuilder.toString()+"】"); } catch (UnsupportedEncodingException e) { throw e; } catch (MalformedURLException e) { throw e; } catch (IOException e) { throw e; }finally { try{ if (readInfo != null) { readInfo.close(); } if (byteOut != null) { byteOut.close(); } if (out != null) { out.close(); } if (conn != null) { conn.disconnect(); } }catch(Exception e){ System.out.println("关闭链接出错!"+e.getMessage()); } } return strBuilder.toString(); } }
时间: 2024-10-13 21:52:22