1.post请求 参数为?a=1&b=2&c=3
2.post请求 参数为{“a”:1,“b”:2,“c”:3} JSON格式
package cn.test.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import com.alibaba.fastjson.JSONObject;
public class URLConnection {
/**
* post请求封装 参数为?a=1&b=2&c=3
* @param path 接口地址
* @param Info 参数
* @return
* @throws IOException
*/
public static JSONObject postResponse(String path,String Info) throws IOException{
//1, 得到URL对象
URL url = new URL(path);
//2, 打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//3, 设置提交类型
conn.setRequestMethod("POST");
//4, 设置允许写出数据,默认是不允许 false
conn.setDoOutput(true);
conn.setDoInput(true);//当前的连接可以从服务器读取内容, 默认是true
//5, 获取向服务器写出数据的流
OutputStream os = conn.getOutputStream();
//参数是键值队 , 不以"?"开始
os.write(Info.getBytes());
//os.write("googleTokenKey=&username=admin&password=5df5c29ae86331e1b5b526ad90d767e4".getBytes());
os.flush();
//6, 获取响应的数据
//得到服务器写回的响应数据
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
String str = br.readLine();
JSONObject json = JSONObject.parseObject(str);
System.out.println("响应内容为: " + json);
return json;
}
/**
* post请求封装 参数为{"a":1,"b":2,"c":3}
* @param path 接口地址
* @param Info 参数
* @return
* @throws IOException
*/
public static JSONObject postResponse(String path,JSONObject Info) throws IOException{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(path);
post.setHeader("Content-Type", "application/json");
post.addHeader("Authorization", "Basic YWRtaW46");
String result = "";
try {
StringEntity s = new StringEntity(Info.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
post.setEntity(s);
// 发送请求
HttpResponse httpResponse = client.execute(post);
// 获取响应输入流
InputStream inStream = httpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
result = strber.toString();
System.out.println(result);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
System.out.println("请求服务器成功,做相应处理");
} else {
System.out.println("请求服务端失败");
}
} catch (Exception e) {
System.out.println("请求异常");
throw new RuntimeException(e);
}
return JSONObject.parseObject(result);
}
}
//main
public static void main(String[] args) {
try {
JSONObject json = new JSONObject();
//第一种post请求 参数为 a=1&b=2&c=3
json = URLConnection.postResponse("https://www.apiopen.top/weatherApi", "city=上海&&test=111");
System.out.println("json的数据为:" + json);
//第二种post请求 参数为 json
json.put("test", "参数1");
json.put("test2", "参数2");
json = URLConnection.postResponse("http://url", json);
} catch (IOException e) {
e.printStackTrace();
}
}