Skip to content

Commit c814409

Browse files
重构请求方法
1 parent a1dbbde commit c814409

File tree

6 files changed

+110
-81
lines changed

6 files changed

+110
-81
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.midream.sheep.api.http;
2+
3+
import com.midream.sheep.swcj.pojo.ExecuteValue;
4+
5+
import java.io.IOException;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
9+
public class HTTPTool {
10+
11+
public static HttpURLConnection getHttpURLConnection(ExecuteValue executeValue) throws IOException {
12+
URL url = new URL(executeValue.getUrl());
13+
//得到连接对象
14+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
15+
//设置请求类型
16+
con.setRequestMethod(executeValue.getType().getValue());
17+
//设置请求需要返回的数据类型和字符集类型
18+
con.setRequestProperty("Content-Type", "application/json;charset=GBK");
19+
con.setRequestProperty("cookie", executeValue.getCookies());
20+
con.setRequestProperty("user-agent", executeValue.getUserAge());
21+
//set the request cookie
22+
con.setRequestProperty("cookie", executeValue.getCookies());
23+
//set the request timeout
24+
con.setConnectTimeout(Integer.parseInt(executeValue.getTimeout()));
25+
//set the request method
26+
con.setRequestProperty("method", executeValue.getType().getValue());
27+
//set the values
28+
con.setRequestProperty("connection", "Keep-Alive");
29+
//允许写出
30+
con.setDoOutput(true);
31+
//允许读入
32+
con.setDoInput(true);
33+
//不使用缓存
34+
con.setUseCaches(false);
35+
return con;
36+
}
37+
}

src/main/java/com/midream/sheep/swcj/core/executetool/execute/SRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
* @description request type description class
66
*/
77
public enum SRequest {
8-
GET("GET"),POST("POST");
8+
GET("GET"),
9+
POST("POST"),
10+
DELETE("DELETE"),
11+
HEAD("HEAD"),
12+
PUT("PUT"),
13+
OPTIONS("OPTIONS"),
14+
TRACE("TRACE"),
15+
CONNECT("CONNECT"),
16+
;
917

1018
private String value;
1119

src/main/java/com/midream/sheep/swcj/core/executetool/execute/regularexpression/SWCJregular.java

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package com.midream.sheep.swcj.core.executetool.execute.regularexpression;
22

33
import com.midream.sheep.api.clazz.ClazzBuilder;
4-
import com.midream.sheep.api.clazz.filed.fileds.StringHandler;
4+
import com.midream.sheep.api.http.HTTPTool;
55
import com.midream.sheep.swcj.core.executetool.SWCJExecute;
66
import com.midream.sheep.swcj.data.Constant;
77
import com.midream.sheep.swcj.data.XmlSpecialStrings;
88
import com.midream.sheep.swcj.pojo.ExecuteValue;
9+
import com.midream.sheep.swcj.util.function.StringUtil;
910
import org.w3c.dom.Node;
1011
import org.w3c.dom.NodeList;
1112
import org.xml.sax.InputSource;
1213

1314
import javax.xml.parsers.DocumentBuilderFactory;
1415
import java.io.*;
15-
import java.net.HttpURLConnection;
16-
import java.net.URL;
1716
import java.util.*;
18-
import java.util.logging.Logger;
1917
import java.util.regex.Matcher;
2018
import java.util.regex.Pattern;
2119

@@ -85,66 +83,14 @@ private void putMap(Node name,Map<String,List<String>> values,List<String> list)
8583
values.put(RegConstants.strName, list);
8684
}
8785
}
88-
8986
/**
9087
* 获取字符串
9188
*/
9289
private String getText(ExecuteValue executeValue) {
93-
HttpURLConnection con;
94-
BufferedReader buffer;
95-
StringBuilder resultBuffer;
96-
InputStream inputStream = null;
9790
try {
98-
URL url = new URL(executeValue.getUrl());
99-
//得到连接对象
100-
con = (HttpURLConnection) url.openConnection();
101-
//设置请求类型
102-
con.setRequestMethod(executeValue.getType().getValue());
103-
//设置请求需要返回的数据类型和字符集类型
104-
con.setRequestProperty("Content-Type", "application/json;charset=GBK");
105-
con.setRequestProperty("cookie", executeValue.getCookies());
106-
con.setRequestProperty("user-agent", executeValue.getUserAge());
107-
//set the request cookie
108-
con.setRequestProperty("cookie", executeValue.getCookies());
109-
//set the request timeout
110-
con.setConnectTimeout(Integer.parseInt(executeValue.getTimeout()));
111-
//set the request method
112-
con.setRequestProperty("method", executeValue.getType().getValue());
113-
//set the values
114-
con.setRequestProperty("connection", "Keep-Alive");
115-
//允许写出
116-
con.setDoOutput(true);
117-
//允许读入
118-
con.setDoInput(true);
119-
//不使用缓存
120-
con.setUseCaches(false);
121-
//得到响应码
122-
int responseCode = con.getResponseCode();
123-
124-
if (responseCode == HttpURLConnection.HTTP_OK) {
125-
//得到响应流
126-
inputStream = con.getInputStream();
127-
//将响应流转换成字符串
128-
resultBuffer = new StringBuilder();
129-
String line;
130-
buffer = new BufferedReader(new InputStreamReader(inputStream, "GBK"));
131-
while ((line = buffer.readLine()) != null) {
132-
resultBuffer.append(line);
133-
}
134-
return resultBuffer.toString();
135-
}
136-
} catch (Exception e) {
137-
Logger.getLogger(SWCJregular.class.getName()).warning(e.getMessage());
138-
e.printStackTrace();
139-
} finally {
140-
if (inputStream != null) {
141-
try {
142-
inputStream.close();
143-
} catch (IOException e) {
144-
e.printStackTrace();
145-
}
146-
}
91+
return StringUtil.getStringByStream(new BufferedReader(new InputStreamReader(HTTPTool.getHttpURLConnection(executeValue).getInputStream(), "GBK")));
92+
} catch (IOException e) {
93+
throw new RuntimeException(e);
14794
}
148-
return "";
14995
}
15096
}

src/main/java/com/midream/sheep/swcj/core/factory/parse/bystr/BetterXmlParseTool.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010
import com.midream.sheep.swcj.pojo.enums.ChooseStrategy;
1111
import com.midream.sheep.swcj.pojo.swc.ReptileUrl;
1212
import com.midream.sheep.swcj.pojo.swc.RootReptile;
13+
import com.midream.sheep.swcj.util.function.StringUtil;
1314

1415
import java.io.File;
15-
import java.io.IOException;
16-
import java.io.InputStream;
1716
import java.lang.reflect.InvocationTargetException;
18-
import java.nio.file.Files;
1917
import java.util.*;
2018
import java.util.logging.Logger;
2119

2220
public class BetterXmlParseTool implements SWCJParseI {
2321
@Override
2422
public List<RootReptile> parseXmlFile(File xmlFile, ReptileConfig rc) {
25-
return parseStringXml(getStringByStream(xmlFile), rc);
23+
return parseStringXml(StringUtil.getStringByStream(xmlFile), rc);
2624
}
2725

2826
@Override
@@ -185,21 +183,4 @@ private String[] parseTag(String xmlString, String startTag, String endTag) {
185183
return swcs.toArray(new String[0]);
186184
}
187185

188-
/**
189-
* 读取文件
190-
* */
191-
private String getStringByStream(File xmlFile) {
192-
//根据File获取xml文件文本
193-
StringBuilder sb = new StringBuilder();
194-
try (InputStream is = Files.newInputStream(xmlFile.toPath())) {
195-
byte[] bytes = new byte[1024];
196-
int len;
197-
while ((len = is.read(bytes)) != -1) {
198-
sb.append(new String(bytes, 0, len));
199-
}
200-
} catch (IOException e) {
201-
Logger.getLogger(BetterXmlParseTool.class.getName()).warning(e.getMessage());
202-
}
203-
return sb.toString();
204-
}
205186
}

src/main/java/com/midream/sheep/swcj/pojo/buildup/SWCJMethod.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
* @author midreamsheep
88
*/
99
public class SWCJMethod {
10+
//方法额外的配置文件
11+
private String config;
12+
//请求类型
1013
private String requestType;
14+
//方法名
1115
private String name;
1216
private String methodName;
1317
private List<MethodHandler> vars;
@@ -34,6 +38,14 @@ public String getRequestType() {
3438
return requestType;
3539
}
3640

41+
public String getConfig() {
42+
return config;
43+
}
44+
45+
public void setConfig(String config) {
46+
this.config = config;
47+
}
48+
3749
public void setRequestType(String requestType) {
3850
this.requestType = requestType;
3951
}

src/main/java/com/midream/sheep/swcj/util/function/StringUtil.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package com.midream.sheep.swcj.util.function;
22

3+
import com.midream.sheep.api.http.HTTPTool;
4+
import com.midream.sheep.swcj.core.executetool.execute.regularexpression.SWCJregular;
5+
import com.midream.sheep.swcj.core.factory.parse.bystr.BetterXmlParseTool;
36
import com.midream.sheep.swcj.data.Constant;
47
import com.midream.sheep.swcj.data.ReptileConfig;
58
import com.midream.sheep.swcj.pojo.buildup.SWCJMethod;
69
import com.midream.sheep.swcj.pojo.swc.ReptileUrl;
710
import com.midream.sheep.swcj.pojo.swc.RootReptile;
811

12+
import java.io.*;
13+
import java.net.HttpURLConnection;
14+
import java.nio.file.Files;
915
import java.util.HashMap;
1016
import java.util.List;
1117
import java.util.Map;
1218
import java.util.Random;
19+
import java.util.logging.Logger;
1320

1421
/**
1522
* @author midreamsheep
@@ -86,4 +93,42 @@ public static String getExecuteCharacter(ReptileUrl ru, List<String> injection,
8693
}
8794
return templet.replace("#[in]",(Constant.nullString.contentEquals(inj))?Constant.nullString:inj.substring(0, inj.length() - 1));
8895
}
96+
/**
97+
* 读取文件
98+
* */
99+
public static String getStringByStream(File xmlFile) {
100+
try {
101+
return getStringByStream(Files.newInputStream(xmlFile.toPath()));
102+
} catch (IOException e) {
103+
throw new RuntimeException(e);
104+
}
105+
}
106+
public static String getStringByStream(InputStream is) {
107+
try (InputStream inputStream = is) {
108+
//根据File获取xml文件文本
109+
StringBuilder sb = new StringBuilder();
110+
byte[] bytes = new byte[1024];
111+
int len;
112+
while ((len = inputStream.read(bytes)) != -1) {
113+
sb.append(new String(bytes, 0, len));
114+
}
115+
return sb.toString();
116+
}catch (IOException e){
117+
throw new RuntimeException(e);
118+
}
119+
}
120+
121+
public static String getStringByStream(BufferedReader gbk) {
122+
StringBuilder sb;
123+
try (BufferedReader input = gbk) {
124+
String line;
125+
sb = new StringBuilder();
126+
while ((line = input.readLine()) != null) {
127+
sb.append(line);
128+
}
129+
return sb.toString();
130+
} catch (IOException e) {
131+
throw new RuntimeException(e);
132+
}
133+
}
89134
}

0 commit comments

Comments
 (0)