📄 socketprovision.java
字号:
/*
* Created on 2004-8-12
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.gctech.misc.util;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
/**
* <p>Title:</p>
* <p>Description:</p>
* <p>Copyright: Copyright (c) Gctech 2004-8-12</p>
* <p>Company: 吉芙德资讯有限公司</p>
*
* @version 1.0
* @author liyi
*
*/
public class SocketProvision {
public static void provision(String ip, String port, String filename) {
try {
BufferedOutputStream sender;
Socket socket = new Socket(ip, Integer.parseInt(port));
OutputStream os = socket.getOutputStream();
sender = new BufferedOutputStream(os);
boolean autoflush = true;
Reader is = new FileReader(filename);
//ByteArrayOutputStream bout = new ByteArrayOutputStream();
// Copy the SOAP file to the open connection.
//copy(fin, bout);
//fin.close();
String content = readFileToString(is);
is.close();
//PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 向Web服务器发送一个HTTP请求
// out.print("POST /dsmp/dsmp.wsdl HTTP/1.1");
// out.print("\r\n");
// out.print("Content-Type: text/xml; charset=utf-8");
// out.print("\r\n");
// out.print("SOAPAction: \"http://yourco.com/OrderRelationReq\"");
// out.print("\r\n");
// out.print("User-Agent: Provision");
// out.print("\r\n");
// out.print("Host: 211.136.90.132:8080");
// out.print("\r\n");
// out.print("Content-Length: " + b.getBytes().length);
// out.print("\r\n");
// out.print("Cache-Control: no-cache");
// out.print("\r\n");
// out.print("\r\n\r\n");
// out.println(b);
// out.print("\r\n");
StringBuffer cmd = new StringBuffer();
cmd.append("POST /dsmp/dsmp.wsdl HTTP/1.1\r\n");
cmd.append("Content-Type: text/xml; charset=utf-8\r\n");
cmd.append("SOAPAction: \"http://yourco.com/OrderRelationReq\"\r\n");
cmd.append("User-Agent: Provision\r\n");
cmd.append("Cache-Control: no-cache\r\n");
cmd.append("Content-length: " + content.length() + "\r\n\r\n");
cmd.append(content + "\r\n");
System.out.println(cmd.toString());
sender.write(cmd.toString().getBytes(), 0, cmd.toString().length());
sender.flush();
os.close();
sender.close();
//读取服务器的应答
boolean loop = true;
StringBuffer sb = new StringBuffer(8096);
while (loop) {
if (in.ready()) {
int i = 0;
while (i != -1) {
i = in.read();
sb.append((char) i);
}
loop = false;
}
Thread.currentThread().sleep(50);
}
// 把应答显示到控制台
System.out.println(sb.toString());
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy(InputStream in, OutputStream out) throws IOException {
// do not allow other threads to read from the
// input or write to the output while copying is
// taking place
synchronized (in) {
synchronized (out) {
byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1)
break;
out.write(buffer, 0, bytesRead);
}
}
}
}
public static String readFileToString(Reader is) throws IOException{
StringBuffer sb = new StringBuffer();
char[] b = new char[1024];
int n;
while((n = is.read(b))>0){
sb.append(b,0,n);
}
return sb.toString();
}
public static void main(String[] args) {
provision("211.136.90.132", "8080", "UnSubscrib.xml");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -