package com.handmobi.hangup.common.utils;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpMocker {
public static String requestMockGet(String requestUrl,String[]... params){
String destUrl = requestUrl;
String paramStr = null;
if(null!=params){
int i =0;
StringBuilder builder = new StringBuilder();
for(String[] param : params){
if(i==0){
++i;
builder.append(param[0]).append("=").append(param[1]);
}else{
builder.append("&").append(param[0]).append("=").append(param[1]);
}
}
paramStr = builder.toString();
}
URL url;
try {
if( paramStr.length() > 0 ){
url = new URL(destUrl+"?"+paramStr);
}else{
url = new URL(destUrl);
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDefaultUseCaches(false);
connection.connect();
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String s = null;
StringBuilder rspBuilder = new StringBuilder();
while(null != (s = reader.readLine())){
rspBuilder.append(s);
}
in.close();
return rspBuilder.toString();
}catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String requestMockGet(String requestUrl){
String destUrl = requestUrl;
URL url;
try {
url = new URL(destUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDefaultUseCaches(false);
connection.connect();
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
String s = null;
StringBuilder rspBuilder = new StringBuilder();
while(null != (s = reader.readLine())){
rspBuilder.append(s);
}
in.close();
return rspBuilder.toString();
}catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String requestMockGet(String requestUrl,Integer timeout){
String destUrl = requestUrl;
URL url;
try {
url = new URL(destUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDefaultUseCaches(false);
connection.setConnectTimeout(timeout);
connection.connect();
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
String s = null;
StringBuilder rspBuilder = new StringBuilder();
while(null != (s = reader.readLine())){
rspBuilder.append(s);
}
in.close();
return rspBuilder.toString();
}catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String requestMockPost(String requestUrl,String[]... params) throws Exception{
String destUrl = requestUrl;
// System.out.println("request url:"+requestUrl);
URL url;
try {
url = new URL(destUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
JSONObject jsonObj = new JSONObject();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(),"utf-8");
if( null != params && params.length > 0 ){
for(String[] param : params){
jsonObj.put(param[0], param[1]);
}
out.write(jsonObj.toString());
}
out.flush();
out.close();
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
String s = null;
// System.out.print("response:");
StringBuilder rspBuilder = new StringBuilder();
while(null != (s = reader.readLine())){
rspBuilder.append(s);
// System.out.println(s);
}
in.close();
return rspBuilder.toString();
}catch (Exception e) {
throw new Exception(e);
}
}
private static boolean checkJsonString(String jsonString){
try {
JSONObject jsonStr= JSONObject.parseObject(jsonString);
return true;
} catch (Exception e) {
return false;
}
}
public static String requestMockPost(String requestUrl,String jsonString) throws Exception{
String destUrl = requestUrl;
// System.out.println("request url:"+requestUrl);
URL url;
try {
url = new URL(destUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
connection.setConnectTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(),"utf-8");
if( jsonString != null && checkJsonString(jsonString)){
out.write(jsonString);
}
out.flush();
out.close();
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
String s = null;
// System.out.print("response:");
StringBuilder rspBuilder = new StringBuilder();
while(null != (s = reader.readLine())){
rspBuilder.append(s);
// System.out.println(s);
}
in.close();
return rspBuilder.toString();
}catch (Exception e) {
throw new Exception(e);
}
}
}