-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
708 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/main/java/com/khjxiaogu/MiraiSongPlugin/HttpRequestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package com.khjxiaogu.MiraiSongPlugin; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
public class HttpRequestBuilder { | ||
private StringBuilder url; | ||
|
||
List<String[]> headers=new ArrayList<>(); | ||
|
||
public HttpRequestBuilder(String ourl) { | ||
url=new StringBuilder(ourl); | ||
} | ||
public static HttpRequestBuilder create(String host) { | ||
return new HttpRequestBuilder("https://"+host).host(host); | ||
} | ||
public static HttpRequestBuilder create(String protocol,String host) { | ||
return new HttpRequestBuilder(protocol+"://"+host).host(host); | ||
} | ||
public HttpRequestBuilder url(String v) { | ||
url.append(v); | ||
return this; | ||
} | ||
public HttpRequestBuilder url(int v) { | ||
url.append(v); | ||
return this; | ||
} | ||
public HttpRequestBuilder url(char v) { | ||
url.append(v); | ||
return this; | ||
} | ||
public HttpRequestBuilder header(String k,String v) { | ||
headers.add(new String[] {k,v}); | ||
return this; | ||
} | ||
public HttpRequestBuilder referer(String v) { | ||
headers.add(new String[] {"referer",v}); | ||
return this; | ||
} | ||
public HttpRequestBuilder contenttype(String v) { | ||
headers.add(new String[] {"content-type",v}); | ||
return this; | ||
} | ||
public HttpRequestBuilder cookie(String v) { | ||
headers.add(new String[] {"cookie",v}); | ||
return this; | ||
} | ||
public HttpRequestBuilder host(String v) { | ||
headers.add(new String[] {"Host",v}); | ||
return this; | ||
} | ||
public HttpRequestBuilder defUA() { | ||
headers.add(new String[] {"User-Agent", | ||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"}); | ||
return this; | ||
} | ||
public HttpRequestBuilder ua(String v) { | ||
headers.add(new String[] {"User-Agent",v}); | ||
return this; | ||
} | ||
public static class OpenedConnectionBuilder { | ||
HttpURLConnection huc; | ||
public OutputStream os; | ||
public OpenedConnectionBuilder(HttpURLConnection huc,boolean doOut) throws IOException { | ||
super(); | ||
this.huc = huc; | ||
if(doOut) | ||
this.os = huc.getOutputStream(); | ||
} | ||
public OpenedConnectionBuilder(HttpURLConnection huc) { | ||
super(); | ||
this.huc = huc; | ||
} | ||
public OpenedConnectionBuilder send(String s) throws IOException { | ||
if(os==null) | ||
os=huc.getOutputStream(); | ||
os.write(s.getBytes(StandardCharsets.UTF_8)); | ||
return this; | ||
} | ||
public OpenedConnectionBuilder send(byte[] s) throws IOException { | ||
if(os==null) | ||
os=huc.getOutputStream(); | ||
os.write(s); | ||
return this; | ||
} | ||
public String readString() throws IOException { | ||
return new String(Utils.readAll(huc.getInputStream()), StandardCharsets.UTF_8); | ||
} | ||
public JsonObject readJson() throws IOException { | ||
return JsonParser.parseString(new String(Utils.readAll(huc.getInputStream()), StandardCharsets.UTF_8)).getAsJsonObject(); | ||
} | ||
} | ||
private HttpURLConnection openConn() throws IOException { | ||
URL url = new URL(this.url.toString()); | ||
HttpURLConnection huc = (HttpURLConnection) url.openConnection(); | ||
huc.setInstanceFollowRedirects(true); | ||
huc.setConnectTimeout(5000); | ||
huc.setReadTimeout(5000); | ||
for(String[] header:headers) { | ||
huc.setRequestProperty(header[0], header[1]); | ||
} | ||
|
||
return huc; | ||
} | ||
public OpenedConnectionBuilder post(boolean doOutput) throws IOException { | ||
HttpURLConnection huc=openConn(); | ||
huc.setRequestMethod("POST"); | ||
huc.setDoOutput(doOutput); | ||
huc.setDoInput(true); | ||
huc.connect(); | ||
return new OpenedConnectionBuilder(huc,doOutput); | ||
|
||
} | ||
public OpenedConnectionBuilder post() throws IOException { | ||
HttpURLConnection huc=openConn(); | ||
huc.setRequestMethod("POST"); | ||
huc.setDoOutput(true); | ||
huc.setDoInput(true); | ||
huc.connect(); | ||
return new OpenedConnectionBuilder(huc,true); | ||
} | ||
public OpenedConnectionBuilder get(boolean doOutput) throws IOException { | ||
HttpURLConnection huc=openConn(); | ||
huc.setRequestMethod("GET"); | ||
huc.setDoOutput(doOutput); | ||
huc.setDoInput(true); | ||
huc.connect(); | ||
return new OpenedConnectionBuilder(huc,doOutput); | ||
} | ||
public OpenedConnectionBuilder get() throws IOException { | ||
HttpURLConnection huc=openConn(); | ||
huc.setRequestMethod("GET"); | ||
huc.connect(); | ||
return new OpenedConnectionBuilder(huc,false); | ||
} | ||
public OpenedConnectionBuilder open(String met,boolean doInput,boolean doOutput) throws IOException { | ||
HttpURLConnection huc=openConn(); | ||
huc.setRequestMethod(met); | ||
huc.setDoOutput(doOutput); | ||
huc.setDoInput(doInput); | ||
huc.connect(); | ||
return new OpenedConnectionBuilder(huc,doOutput); | ||
} | ||
|
||
|
||
} |
114 changes: 114 additions & 0 deletions
114
src/main/java/com/khjxiaogu/MiraiSongPlugin/JsonBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.khjxiaogu.MiraiSongPlugin; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
|
||
public class JsonBuilder { | ||
|
||
public JsonBuilder() { | ||
} | ||
public static JsonArrayBuilder<JsonArray> array(){ | ||
return new JsonArrayBuilder<>(null); | ||
} | ||
public static JsonObjectBuilder<JsonObject> object(){ | ||
return new JsonObjectBuilder<>(null); | ||
} | ||
public static class JsonArrayBuilder<T> { | ||
JsonArray jo=new JsonArray(); | ||
T parent; | ||
private JsonArrayBuilder(T par) { | ||
parent=par; | ||
} | ||
public T end() { | ||
if(parent==null) | ||
return (T) jo; | ||
return parent; | ||
} | ||
public JsonArrayBuilder<T> add(String v){ | ||
jo.add(v); | ||
return this; | ||
} | ||
public JsonArrayBuilder<T> add(Number v){ | ||
jo.add(v); | ||
return this; | ||
} | ||
public JsonArrayBuilder<T> add(boolean v){ | ||
jo.add(v); | ||
return this; | ||
} | ||
public JsonArrayBuilder<T> add(Character v){ | ||
jo.add(v); | ||
return this; | ||
} | ||
public JsonArrayBuilder<T> add(JsonElement v){ | ||
jo.add(v); | ||
return this; | ||
} | ||
public JsonObjectBuilder<JsonArrayBuilder<T>> object(){ | ||
JsonObjectBuilder<JsonArrayBuilder<T>> job= new JsonObjectBuilder<>(this); | ||
this.add(job.get()); | ||
return job; | ||
} | ||
public JsonArrayBuilder<JsonArrayBuilder<T>> array(){ | ||
JsonArrayBuilder<JsonArrayBuilder<T>> job= new JsonArrayBuilder<>(this); | ||
this.add(job.get()); | ||
return job; | ||
} | ||
public JsonArray get() { | ||
return jo; | ||
}; | ||
public String toString() { | ||
return jo.toString(); | ||
} | ||
} | ||
public static class JsonObjectBuilder<T> { | ||
JsonObject jo=new JsonObject(); | ||
T parent; | ||
private JsonObjectBuilder(T par) { | ||
parent=par; | ||
} | ||
|
||
public T end() { | ||
if(parent==null) | ||
return (T) jo; | ||
return parent; | ||
} | ||
public JsonObjectBuilder<T> add(String k,String v){ | ||
jo.addProperty(k,v); | ||
return this; | ||
} | ||
public JsonObjectBuilder<T> add(String k,Number v){ | ||
jo.addProperty(k,v); | ||
return this; | ||
} | ||
public JsonObjectBuilder<T> add(String k,boolean v){ | ||
jo.addProperty(k,v); | ||
return this; | ||
} | ||
public JsonObjectBuilder<T> add(String k,Character v){ | ||
jo.addProperty(k, v); | ||
return this; | ||
} | ||
public JsonObjectBuilder<T> add(String k,JsonElement v){ | ||
jo.add(k, v); | ||
return this; | ||
} | ||
public JsonObjectBuilder<JsonObjectBuilder<T>> object(String k){ | ||
JsonObjectBuilder<JsonObjectBuilder<T>> job= new JsonObjectBuilder<JsonObjectBuilder<T>>(this); | ||
this.add(k, job.get()); | ||
return job; | ||
} | ||
public JsonArrayBuilder<JsonObjectBuilder<T>> array(String k){ | ||
JsonArrayBuilder<JsonObjectBuilder<T>> job= new JsonArrayBuilder<>(this); | ||
this.add(k,job.get()); | ||
return job; | ||
} | ||
public JsonObject get() { | ||
return jo; | ||
}; | ||
public String toString() { | ||
return jo.toString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.