62,614
社区成员
发帖
与我相关
我的任务
分享
package sockettest;
import java.net.*;
import java.io.*;
/**
*
* @author 雁过留声
*/
public class Connect {
static Socket socket;
public static boolean Connect(String ip, int port) throws Exception
{
try {
socket = new Socket(ip, port);
PrintStream out;
BufferedReader in;
out = new PrintStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
StringBuffer sb = new StringBuffer("POST /netfilter/UserLogon.asmx HTTP/1.1\r\n");
sb.append("Host: auth.stu.edu.cn\r\n");
sb.append("Content-Type: text/xml; charset=utf-8\r\n");
sb.append("Content-Length: 466\r\n");
sb.append("SOAPAction: \"http://tempuri.org/ClientLogon\"\r\n\r\n");
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.append("<soap:Body>");
sb.append("<ClientLogon xmlns=\"http://tempuri.org/\">");
sb.append("<userName>07lslin1</userName>");
sb.append("<password>123</password>");
sb.append("<userIp>10.14.2.146</userIp>");
sb.append("<hostName>null</hostName>");
sb.append("<userMac>null</userMac>");
sb.append("<userAgent>null</userAgent>");
sb.append("<isVisiteForeign>true</isVisiteForeign>");
sb.append("</ClientLogon>");
sb.append("</soap:Body>\r\n");
sb.append("</soap:Envelope>\r\n");
OutputStream socketOut = socket.getOutputStream();
socketOut.write(sb.toString().getBytes());
InputStream socketIn = socket.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = -1;
while((len=socketIn.read(buff))!=-1)
{
buffer.write(buff, 0, len);
}
System.out.println(new String (buffer.toByteArray()));
return true;
} catch (IOException ex) {//连接不成功
System.out.println("异常:"+ex.toString());
}
return false;
}
public static void main(String[] rgs) throws Exception
{
boolean result = Connect("auth.stu.edu.cn",80);
System.out.println(result);
}
}