待解析字符串
String url="http://120.132.0.117:8080/yun90/article/GetRelatedArticles";
代码
package com.basic;
public class Parse05Bean {
public String scheme;
public String ip;
public String port;
public String context;
public String urlPattern;
}
package com.basic;
public class Parse05 {
public static void main(String[] args) {
String post = "POST http://120.132.0.117:8080/yun90/article/GetRelatedArticles HTTP/1.1\r\nHost: blog.csdn.net\r\nConnection: keep-alive\r\n\r\npageindex=2&articleId=23462041";
String url="http://120.132.0.117:8080/yun90/article/GetRelatedArticles";
Parse05Bean bean=new Parse05Bean();
/*
* 一、按位置切割请求协议:即http
*/
int index=url.indexOf("://");
String scheme=url.substring(0,index);
bean.scheme=scheme;
System.out.println("scheme="+scheme);
url=url.substring(index+3);
System.out.println("new url="+url+"\r\n");
/*
* 二、解析ip
*/
index=url.indexOf(":");
String ip=url.substring(0,index);
bean.ip=ip;
System.out.println("ip="+ip);
url=url.substring(index+3);
System.out.println("new url="+url+"\r\n");
/*
* 三、解析端口
*/
index=url.indexOf("/");
String port=url.substring(0,index);
bean.port=port;
System.out.println("port="+port);
url=url.substring(index+3);
System.out.println("new url="+url+"\r\n");
/*
* 四、解析应用名和资源路径
*/
index=url.indexOf("/");
String context=url.substring(0,index);
bean.context=context;
System.out.println("context="+context);
String urlPattern=url.substring(index+3);
bean.urlPattern=urlPattern;
System.out.println("urlPattern="+urlPattern);
System.out.println("-----------------------取数据----------------------");
System.out.println("bean.ip="+bean.ip);
System.out.println("bean.port="+bean.port);
}
}
网友评论