java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求,
package
main.utils;
import
java.io.*;
import
java.net.HttpURLConnection;
import
java.net.URL;
public
class
HttpUtilTest {
Log log =
new
Log(
this
.getClass());
//初始化日志类
/**
* @作用 使用urlconnection
* @param url
* @param Params
* @return
* @throws IOException
*/
public
String sendPost(String url,String Params)
throws
IOException{
OutputStreamWriter out =
null
;
BufferedReader reader =
null
;
String response=
""
;
try
{
URL httpUrl =
null
;
//HTTP URL类 用这个类来创建连接
//创建URL
httpUrl =
new
URL(url);
//建立连接
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
|