日期:2014-05-19 浏览次数:20766 次
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.Socket; import java.net.URL; import java.net.URLConnection; import java.security.GeneralSecurityException; import java.security.SecureRandom; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import org.apache.commons.logging.*; public class HttpPostTest_Socket { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String url="https://10.63.11.34/cbmui/en_US/themes/excel/index.htm"; SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault(); String reqMsg= getFirestRequestMsg(url); Socket socket=null; try { System.out.println("message:"+reqMsg); socket = ssf.createSocket("10.63.11.43", 443); PrintWriter tOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); tOut.write(reqMsg); tOut.println(); tOut.flush(); System.out.println("Message sent successfully."); BufferedReader tIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); String tLine = null; StringBuilder sb=new StringBuilder(); while ((tLine = tIn.readLine()) != null) { sb.append(tLine).append("\n"); } System.out.println("Response:"+sb.toString()); }