📄 ophttp.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: OpHttp.java
package carven;
import java.io.*;
import java.net.Socket;
import java.util.Date;
// Referenced classes of package carven:
// OpStream
public class OpHttp
{
private String httpHead;
private byte httpContent[];
private String requestURI;
private InputStream inStream;
private OutputStream outStream;
public String encode;
public String contentType;
public OpHttp(InputStream inStream, OutputStream outStream)
throws Exception
{
requestURI = null;
encode = "gb2312";
contentType = "text/vnd.html";
this.inStream = inStream;
this.outStream = outStream;
ParseHttpHeaderAndContent();
}
public String GetRequestURI()
{
if (requestURI == null)
{
int pos1 = httpHead.toLowerCase().indexOf("get ");
int pos2 = httpHead.indexOf(" ", pos1 + 4);
requestURI = httpHead.substring(pos1 + 4, pos2);
}
return requestURI;
}
public String GetVisitMethod()
{
if (httpHead.toLowerCase().indexOf("get ") > -1)
return "get";
else
return "post";
}
public String QueryString(String param)
{
param = param.toLowerCase();
String requestURI = GetRequestURI();
String lowerRequerstRUI = requestURI.toLowerCase();
int pos1 = lowerRequerstRUI.indexOf("?", 0);
if (pos1 == -1)
return null;
int pos2 = lowerRequerstRUI.indexOf("?" + param + "=", pos1);
if (pos2 == -1)
pos2 = lowerRequerstRUI.indexOf("&" + param + "=", pos1);
if (pos2 == -1)
return null;
int len = 2 + param.length();
int pos3 = lowerRequerstRUI.indexOf("&", pos2 + len);
String result;
if (pos3 == -1)
result = requestURI.substring(pos2 + len);
else
result = requestURI.substring(pos2 + len, pos3);
return result;
}
public String GetContent()
throws Exception
{
return new String(httpContent);
}
public String GetHead()
throws Exception
{
return httpHead;
}
public byte[] GetByteContent()
throws Exception
{
return httpContent;
}
public void ReplyMsg(String msg)
throws Exception
{
int len = msg.getBytes(encode).length;
msg = "HTTP/1.1 200 OK\r\nContent-Type: " + contentType + "; charset=" + encode + "\r\n" + "Content-Length: " + len + "\r\n" + "\r\n" + msg;
if (encode.equals("utf-8"))
outStream.write((new String(msg.getBytes("gb2312"), "ISO8859_1")).getBytes("ISO8859_1"));
else
outStream.write(msg.getBytes(encode));
outStream.flush();
}
private String ParseURI(String str)
{
int pos1 = str.toLowerCase().indexOf("get ");
int pos2 = str.indexOf(" ", pos1 + 4);
return str.substring(pos1 + 4, pos2);
}
private void ParseHttpHeaderAndContent()
throws Exception
{
char rn[] = {
'\r', '\n'
};
DataInputStream bf = new DataInputStream(inStream);
int lenContent = -1;
String strLine;
while (!(strLine = bf.readLine()).equals(""))
{
String tmp = strLine.toLowerCase();
if (tmp.indexOf("content-length:") == 0)
lenContent = Integer.parseInt(strLine.substring(16));
if (tmp.indexOf("get") == 0)
requestURI = ParseURI(tmp);
httpHead = (httpHead != null ? httpHead : "") + strLine + new String(rn);
}
if (lenContent > -1)
{
byte tmpByte[] = new byte[lenContent];
bf.read(tmpByte);
httpContent = tmpByte;
} else
{
httpContent = new byte[0];
}
}
private int GetContentLength(String httpHead)
{
int pos1 = httpHead.indexOf("content-length:");
if (pos1 == -1)
{
return -1;
} else
{
int pos2 = httpHead.indexOf("\r\n", pos1 + 15);
return Integer.parseInt(httpHead.substring(pos1 + 15, pos2).trim());
}
}
public void Close()
{
try
{
inStream.close();
outStream.close();
}
catch (Exception exception) { }
}
private static byte[] QueryWeb(String visitAddress, String postContent, boolean bPost)
throws Exception
{
String httpMsg;
Socket so;
InputStream inStream;
OutputStream outStream;
String dest[] = ParseLink(visitAddress);
httpMsg = null;
if (bPost)
httpMsg = "POST " + visitAddress + " HTTP/1.1\r\n" + "Content-Type: text/xml\r\n" + "Content-Length:" + postContent.getBytes("gb2312").length + "\r\n" + "\r\n" + postContent;
else
httpMsg = "GET " + dest[2] + " HTTP/1.1\r\n" + "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*\r\n" + "Accept-Language: zh-cn\r\n" + "Host: " + dest[0] + ":" + dest[1] + "\r\n\r\n";
System.out.println(httpMsg);
so = new Socket(dest[0], Integer.parseInt(dest[1]));
so.setSoTimeout(10000);
inStream = so.getInputStream();
outStream = so.getOutputStream();
byte abyte0[];
outStream.write(httpMsg.getBytes("gb2312"));
outStream.flush();
OpHttp opHttp = new OpHttp(inStream, outStream);
abyte0 = opHttp.GetByteContent();
inStream.close();
outStream.close();
so.close();
return abyte0;
}
public static String VisitWeb(String visitAddress, String postContent)
throws Exception
{
return new String(QueryWeb(visitAddress, postContent, true), "gb2312");
}
public static String VisitWeb(String visitAddress)
throws Exception
{
return new String(QueryWeb(visitAddress, null, false), "gb2312");
}
public static byte[] VisitWebByte(String visitAddress, String postContent)
throws Exception
{
return QueryWeb(visitAddress, postContent, true);
}
public static byte[] VisitWebByte(String visitAddress)
throws Exception
{
return QueryWeb(visitAddress, null, false);
}
public static String[] ParseLink(String linkStr)
{
int pos1 = linkStr.indexOf("/", 7);
String headStr;
String bodyStr;
if (pos1 == -1)
{
headStr = linkStr.substring(0);
bodyStr = "";
} else
{
headStr = linkStr.substring(0, pos1);
bodyStr = linkStr.substring(pos1);
}
pos1 = headStr.indexOf("http://");
int pos2 = headStr.indexOf(":", pos1 + 7);
String ip;
String port;
if (pos2 == -1)
{
ip = headStr.substring(pos1 + 7);
port = "80";
} else
{
ip = headStr.substring(pos1 + 7, pos2);
port = headStr.substring(pos2 + 1);
}
String result[] = new String[3];
result[0] = ip;
result[1] = port;
result[2] = bodyStr;
return result;
}
public static String ReadHttpMsg(InputStream inStream)
throws Exception
{
int bodyLength = 0;
for (String tmp = null; (tmp = OpStream.ReadLine(inStream)).length() > 0;)
{
if (tmp.indexOf("Content-Length:") > -1)
bodyLength = Integer.parseInt(tmp.substring(16));
if (tmp.equals(""))
break;
}
byte buffer[] = OpStream.ReadBytes(inStream, bodyLength);
if (buffer == null)
return "";
else
return new String(buffer);
}
public static String WriteHttpMsg(String httpTemplate, String body)
throws Exception
{
String result = httpTemplate;
int pos = result.indexOf('\r', result.length() - 2);
if (pos > -1)
result = result.substring(0, pos);
body = body + "\r\n";
result = result.replaceFirst("##Date##", (new Date()).toString());
result = result.replaceFirst("##BodyLength##", String.valueOf(body.getBytes().length));
result = result.replaceFirst("##BodyContent##", body);
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -