v0.00001
This commit is contained in:
@@ -1,15 +1,30 @@
|
|||||||
package org.pavlik.helpers;
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.pavlik.helpers.RestHelper;
|
||||||
|
//package org.pavlik.helpers;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
RestHelper Rest = new RestHelper("client-keystore3.jks","JKS","123321","2","truststore.jks","JKS","123321");
|
RestHelper Rest = new RestHelper(
|
||||||
String cyrillicString = "какашка";
|
"client-keystore3.jks",
|
||||||
// try {
|
"JKS",
|
||||||
// cyrillicString = URLEncoder.encode(cyrillicString, "UTF-8");
|
"123321",
|
||||||
// } catch (UnsupportedEncodingException e) {
|
"2",
|
||||||
// throw new RuntimeException(e);
|
"truststore.jks",
|
||||||
// }
|
"JKS",
|
||||||
Rest.queryPostRaw("https://192.168.200.10/index.php?koko=1",cyrillicString);
|
"123321");
|
||||||
|
|
||||||
|
|
||||||
|
String queryUrl = "https://192.168.200.10/index.php?kokoko=1";
|
||||||
|
String queryBody = "{\"k\":\"val\"}";
|
||||||
|
HashMap<String,String> queryHeaders = new HashMap<>();
|
||||||
|
queryHeaders.put("CONTENT-TYPE","text/plain2; charset=UTF-8");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
HttpResponse resp = Rest.queryPostRaw(queryUrl,queryBody,queryHeaders);
|
||||||
|
String respBody = Rest.getRespBody(resp);
|
||||||
|
System.out.println(respBody);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ import org.apache.http.util.EntityUtils;
|
|||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
|
||||||
public class RestHelper {
|
public class RestHelper {
|
||||||
@@ -92,29 +93,36 @@ public class RestHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void map2headers(HttpPost query,HashMap<String, String> HeaderMap) {
|
||||||
|
for (String key : HeaderMap.keySet()) {
|
||||||
|
query.setHeader(key,HeaderMap.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRespBody(HttpResponse resp){
|
||||||
|
try {
|
||||||
|
return EntityUtils.toString(resp.getEntity(), this.codepage);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void queryPostRaw(String queryUrl, String queryBody) {
|
/**
|
||||||
HttpResponse response = null;
|
* Отправляет запрос "как есть" строкой
|
||||||
String responseBody = null;
|
* @param queryUrl
|
||||||
|
* @param queryBody
|
||||||
|
*/
|
||||||
|
public HttpResponse queryPostRaw(String queryUrl, String queryBody, HashMap<String, String> headerMap) {
|
||||||
HttpPost query = new HttpPost(queryUrl);
|
HttpPost query = new HttpPost(queryUrl);
|
||||||
query.setHeader("р1","yes"); //заголовок
|
if (headerMap != null) this.map2headers(query, headerMap);
|
||||||
query.setHeader("CONTENT-TYPE","text/plain; charset=UTF-8"); //заголовок
|
|
||||||
try {
|
try {
|
||||||
query.setEntity(new StringEntity(queryBody,this.codepage)); //тело
|
query.setEntity(new StringEntity(queryBody,this.codepage)); //тело
|
||||||
response = this.httpClient.execute(query);
|
return this.httpClient.execute(query);
|
||||||
responseBody = EntityUtils.toString(response.getEntity(), this.codepage);
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
System.out.println(response.getStatusLine());
|
|
||||||
|
|
||||||
System.out.println("Response body: " + responseBody);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user