1 Star 0 Fork 22

linving/WebJava

forked from 老馬/WebJava 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Request.java 2.64 KB
一键复制 编辑 原始数据 按行查看 历史
Ma 提交于 2015-06-12 09:41 +08:00 . 添加Demo
package Web;
import java.util.HashMap;
import java.util.StringTokenizer;
/**
* 处理接受到的数据
*
* @author Ma
*/
public class Request {
public HashMap Header;
public HashMap Arguments;
public HashMap Cookies;
public String Path;
public String Method;
public String RemoteIP;
/**
* 处理接收数据
* @param receive 接受
*/
public Request(String receive) {
StringTokenizer stringTokenizer = new StringTokenizer(receive, "\n");
String[] receiveArray = new String[stringTokenizer.countTokens()];
HashMap<String, String> hashMap = new HashMap<String, String>();
this.Header = new HashMap();
this.Arguments = new HashMap();
this.Cookies = new HashMap();
String[] URLStrings = stringTokenizer.nextToken().split(" ");
this.Method = URLStrings[0];//请求方式
String href = URLStrings[1]; //URL
String[] hrefSplit = href.split("\\?", 2);
this.Path = hrefSplit[0];
//分析URL传值
if (hrefSplit.length > 1) {
String query = hrefSplit[1];
this.Arguments = ResolveQuery.query(query);
}
while (stringTokenizer.hasMoreTokens()) {
String temp = stringTokenizer.nextToken().trim();
//长度小于1说明是个空白行,说明header可以结束了,如果是POST请求下面的就是参数了
if (temp.length() < 1) {
break;
}
String[] split = temp.split(":", 2);
if (split.length < 2) {
;
}
if (split[0].toLowerCase().equals("cookie")) {
//解析Cookie,不记录到Header中
Cookie.analysis(split[1].trim());
continue;
}
this.Header.put(split[0], split[1].trim());
}
while (stringTokenizer.hasMoreTokens()) {
String temp = stringTokenizer.nextToken().trim();
//长度小于1说明是个空白行,说明header可以结束了,如果是POST请求下面的就是参数了
if (temp.length() < 1) {
continue;
}
this.Arguments.putAll(ResolveQuery.query(temp));
}
}
/**
* 获取用户远程IP
* @return 远程IP
*/
public String getRemoteIP() {
if(this.Header.containsKey("True-ip")){
this.RemoteIP = (String) this.Header.get("True-ip");
}
return this.RemoteIP;
}
/**
* 设置远程IP
* @param RemoteIP
*/
public void setRemoteIP(String RemoteIP) {
this.RemoteIP = RemoteIP;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/linving/WebJava.git
git@gitee.com:linving/WebJava.git
linving
WebJava
WebJava
master

搜索帮助