使用的jar包
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public abstract class AbstractService {
public void post_json(String url, String jsonString) {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
HttpPost request = new HttpPost(url);
StringEntity params = new StringEntity(jsonString, "UTF-8");
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
StringBuffer sb = new StringBuffer();
String data = null;
while ((data = in.readLine()) != null) {
sb.append(data);
sb.append("\n");
}
System.out.println(sb.toString());
if (in != null) {
in.close();
}
} catch (Exception e) {
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public abstract class AbstractService {
public void post_json(String url, String jsonString) {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
HttpPost request = new HttpPost(url);
StringEntity params = new StringEntity(jsonString, "UTF-8");
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
StringBuffer sb = new StringBuffer();
String data = null;
while ((data = in.readLine()) != null) {
sb.append(data);
sb.append("\n");
}
System.out.println(sb.toString());
if (in != null) {
in.close();
}
} catch (Exception e) {
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}