package com.primeton.esb.httpClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestClient{
	public static void main(String[] args) throws FileNotFoundException, IOException {
		//请求URL地址:httpTransport上的ip端口+代理服务上配的Uri
	//	String urlString = "http://127.0.0.1:9090/RequestInfo"; //Http穿透服务
		
	String urlString = "http://127.0.0.1:9091/HttpToJms"; //HttpToJms中介服务

//		String urlString = "http://127.0.0.1:8088/servletdemo/servlet/jsonCustServlet";//HTTP+JSON的Servlet
//		String urlString = "http://192.168.10.78:9090/com.primeton.esb.provider.custsystem.custJsonService";//HTTP+JSON服务
//		String urlString = "http://192.168.10.78:9090/com.primeton.esb.provider.custsystem.CustMgrService?wsdl";//HTTP+SOAP服务
		
		//请求消息所在文件
        String requestFile  = "http.txt";//Test Http To Jms,当测试HTTP+JSON的Servlet时，内容为：{"custid":1}
//		String requestFile  = "soap.xml";//HTTP+SOAP服务
        
        URL url = new URL(urlString);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        File fileToSend=new File(requestFile);
        byte[] buf=new byte[(int)fileToSend.length()];
        new FileInputStream(requestFile).read(buf);
             
        httpConn.setRequestProperty("Content-Length",String.valueOf( buf.length ) );
        
        httpConn.setRequestProperty("Content-Type","text/plain; charset=utf-8");  
//        httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8"); //HTTP+SOAP服务 
               
//		httpConn.setRequestProperty("ClientId","com.primeton.esb.consumer.custclient");//SAM中“服务消费者域”中“调用客户系统”的应用编码
//		httpConn.setRequestProperty("OperationCode","com.primeton.esb.provider.custsystem.CustMgrService.getCustByID");//SAM中HTTP+SOAP服务的getCustByID操作编码
//		httpConn.setRequestProperty("OperationCode","com.primeton.esb.provider.custsystem.custJsonService.getCust");//SAM中HTTP+JSON服务的getCust操作编码      
        
        httpConn.setRequestMethod( "POST" );
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        
        OutputStream out = httpConn.getOutputStream();
        out.write( buf );
        out.close();
               
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(),"utf-8");      
        BufferedReader in = new BufferedReader(isr);       
        String inputLine;
        StringBuffer sb =new StringBuffer();
        while ((inputLine = in.readLine()) != null){
        	sb.append(inputLine);
        }
        in.close();
        System.out.println(sb.toString());
	}	
}

