Skip to content

Instantly share code, notes, and snippets.

@hcortezr
Created June 7, 2018 04:11
Show Gist options
  • Save hcortezr/c991950621fc078894469ae312cb0461 to your computer and use it in GitHub Desktop.
Save hcortezr/c991950621fc078894469ae312cb0461 to your computer and use it in GitHub Desktop.
[ModPE] HTTPRequest

Example:

function newLevel(){
  clientMessage(new HTTPRequest("http://google.com").get());
}

function procCmd(str){
  let args = str.split(" ");
  let cmd = args.shift();
  
  if(cmd === "get"){
    clientMessage(new HTTPRequest(args.join(" ")).get());
  }else if(cmd === "post"){
    clientMessage(new HTTPRequest(args.join(" ")).post({sentFrom: "ModPE"}));
  }
}```
var HTTPRequest = /** @class */ (function () {
function HTTPRequest(url) {
this.headers = {
"User-Agent": "Mozilla/5.0"
};
this.response = "";
this.responseCode = 0;
this.url = url;
}
HTTPRequest.prototype.addHeader = function (name, value) {
this.headers[name] = value;
return this;
};
HTTPRequest.prototype.getHeader = function (name) {
return this.headers[name];
};
HTTPRequest.prototype.getHeaders = function () {
return Object.assign({}, this.headers);
};
HTTPRequest.prototype.reset = function () {
this.response = "";
this.responseCode = 0;
};
HTTPRequest.prototype.get = function () {
this.reset();
var url = new java.net.URL(this.url);
var con = url.openConnection();
con.setRequestMethod("GET");
for (var name in this.headers)
con.setRequestProperty(name, this.headers[name]);
this.responseCode = con.getResponseCode();
var bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
var line;
while ((line = bufferedReader.readLine()) != null) {
this.response += line + "\n";
}
return this.response;
};
HTTPRequest.prototype.post = function (params) {
this.reset();
var url = new java.net.URL(this.url);
var con = url.openConnection();
con.setRequestMethod("POST");
for (var name in this.headers)
con.setRequestProperty(name, this.headers[name]);
con.setDoOutput(true);
var os = con.getOutputStream();
var param_string = [];
for (var key in params)
param_string.push(key + "=" + encodeURIComponent(params[key]));
param_string = param_string.join("&");
os.write(param_string.split("").map(function (c) { return c.charCodeAt(0); }));
os.flush();
os.close();
this.responseCode = con.getResponseCode();
var bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
var line;
while ((line = bufferedReader.readLine()) != null) {
this.response += line + "\n";
}
return this.response;
};
return HTTPRequest;
}());
declare var java: any;
class HTTPRequest {
protected url: string;
protected headers: any = {
"User-Agent": "Mozilla/5.0"
};
public response: string = "";
public responseCode: number = 0;
constructor(url: string) {
this.url = url;
}
addHeader(name: string, value: string) {
this.headers[name] = value;
return this;
}
getHeader(name: string) {
return this.headers[name];
}
getHeaders() {
return Object.assign({}, this.headers);
}
reset() {
this.response = "";
this.responseCode = 0;
}
get() {
this.reset();
let url = new java.net.URL(this.url);
let con = url.openConnection();
con.setRequestMethod("GET");
for (let name in this.headers) con.setRequestProperty(name, this.headers[name]);
this.responseCode = con.getResponseCode();
let bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
let line;
while ((line = bufferedReader.readLine()) != null) {
this.response += line + "\n";
}
return this.response;
}
post(params: any) {
this.reset();
let url = new java.net.URL(this.url);
let con = url.openConnection();
con.setRequestMethod("POST");
for (let name in this.headers) con.setRequestProperty(name, this.headers[name]);
con.setDoOutput(true);
let os = con.getOutputStream();
let param_string: string | string[] = [];
for (let key in params) param_string.push(key + "=" + encodeURIComponent(params[key]));
param_string = param_string.join("&");
os.write(param_string.split("").map(c => c.charCodeAt(0)));
os.flush();
os.close();
this.responseCode = con.getResponseCode();
let bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
let line;
while ((line = bufferedReader.readLine()) != null) {
this.response += line + "\n";
}
return this.response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment