126
126
public class DemoController extends APIJSONRouterController <Long > { // APIJSONController<Long> {
127
127
private static final String TAG = "DemoController" ;
128
128
129
- // 可以更方便地通过日志排查错误
130
- @ Override
131
- public String getRequestURL () {
129
+ public String getRequestBaseURL () {
132
130
HttpServletRequest httpReq = httpServletRequest ;
133
-
134
- String path = httpReq .getServletPath ();
135
- int index = path .lastIndexOf ("/" );
136
- path = index < 0 ? path : path .substring (0 , index );
137
131
String host = httpReq .getHeader ("origin" );
132
+
138
133
if (StringUtil .isEmpty (host )) {
139
134
host = httpReq .getHeader ("host" );
140
135
String prefix = httpReq .getProtocol ().trim ().toLowerCase ().contains ("https" ) ? "https://" : "http://" ;
141
136
if (StringUtil .isEmpty (host )) {
142
- host = prefix + httpReq .getServerName () + ":" + httpReq .getServerPort ();
143
- } else {
144
- host = prefix + host ;
137
+ return prefix + httpReq .getServerName () + ":" + httpReq .getServerPort ();
145
138
}
139
+
140
+ return prefix + host ;
146
141
}
147
142
148
- return host + path ;
143
+ return host ;
144
+ }
145
+
146
+ public String getRequestPath () {
147
+ String path = httpServletRequest .getServletPath ();
148
+ int index = path .lastIndexOf ("/" );
149
+ path = index < 0 ? path : path .substring (0 , index );
150
+ return path ;
151
+ }
152
+
153
+ // 可以更方便地通过日志排查错误
154
+ @ Override
155
+ public String getRequestURL () {
156
+ return getRequestBaseURL () + getRequestPath ();
149
157
}
150
158
151
159
// @Override
@@ -394,13 +402,13 @@ public String deleteByTag(@PathVariable("tag") String tag, @RequestParam Map<Str
394
402
*/
395
403
@ GetMapping ("get/{request}" )
396
404
public String openGet (@ PathVariable ("request" ) String request , HttpSession session ) {
397
- if (! Log .DEBUG ) { // 一般情况这样简单使用
398
- try {
399
- request = URLDecoder .decode (request , StandardCharsets .UTF_8 );
400
- } catch (Exception e ) {
401
- // Parser会报错
402
- }
405
+ try {
406
+ request = URLDecoder .decode (request , StandardCharsets .UTF_8 );
407
+ } catch (Exception e ) {
408
+ // Parser 会报错
409
+ }
403
410
411
+ if (! Log .DEBUG ) { // 一般情况这样简单使用
404
412
return get (request , session );
405
413
}
406
414
@@ -419,8 +427,26 @@ public String openGet(@PathVariable("request") String request, HttpSession sessi
419
427
}
420
428
}
421
429
422
- newUrl = "http://apijson.cn/api?send=false&redirect=false&type=JSON&decode=true&url="
423
- + url + query + "&json=" + request ;
430
+ if (StringUtil .isNotEmpty (url )) {
431
+ try {
432
+ url = URLEncoder .encode (url , StandardCharsets .UTF_8 );
433
+ } catch (Throwable e ) {
434
+ e .printStackTrace ();
435
+ }
436
+ }
437
+
438
+ String req = request ;
439
+ if (StringUtil .isNotEmpty (req )) {
440
+ try {
441
+ req = URLEncoder .encode (req , StandardCharsets .UTF_8 );
442
+ } catch (Throwable e ) {
443
+ e .printStackTrace ();
444
+ }
445
+ }
446
+
447
+ String baseUrl = getRequestBaseURL ();
448
+ newUrl = baseUrl + "/api/index.html?send=false&redirect=false&type=JSON&decode=true&url="
449
+ + url + query + "&json=" + req ;
424
450
425
451
// httpServletResponse.setHeader("Referer", newUrl);
426
452
// httpServletResponse.setHeader("Redirect-Ref", newUrl);
@@ -429,12 +455,6 @@ public String openGet(@PathVariable("request") String request, HttpSession sessi
429
455
e .printStackTrace ();
430
456
}
431
457
432
- try {
433
- request = URLDecoder .decode (request , StandardCharsets .UTF_8 );
434
- } catch (Exception e ) {
435
- // Parser会报错
436
- }
437
-
438
458
JSONObject rsp = newParser (session , GET ).parseResponse (request );
439
459
rsp .put ("@link" , newUrl );
440
460
String str = JSON .format (rsp ); // rsp.toJSONString();
0 commit comments