由于不建议使用Apache库,因此对于那些想使用的人HttpURLConncetion,我编写了此类,以借助以下答案发送Get和Post Request :
public class WebService {
static final String COOKIES_HEADER = "Set-COOKIE";
static final String COOKIE = "COOKIE";
static COOKIEManager msCOOKIEManager = new COOKIEManager();
private static int responseCode;
public static String sendPost(String requestURL, String urlParameters) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
if (msCOOKIEManager.getCOOKIEStore().getCOOKIEs().size() > 0) {
//While joining the COOKIEs, use ',' or ';' as needed. Most of the server are using ';'
conn.setRequestProperty(COOKIE ,
TextUtils.join(";", msCOOKIEManager.getCOOKIEStore().getCOOKIEs()));
}
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
if (urlParameters != null) {
writer.write(urlParameters);
}
writer.flush();
writer.close();
os.close();
Map> headerFields = conn.getHeaderFields();
List COOKIEsHeader = headerFields.get(COOKIES_HEADER);
if (COOKIEsHeader != null) {
for (String COOKIE : COOKIEsHeader) {
msCOOKIEManager.getCOOKIEStore().add(null, HttpCOOKIE.parse(COOKIE).get(0));
}
}
setResponseCode(conn.getResponseCode());
if (getResponseCode() == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
// HTTP GET request
public static String sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla");
/*
* https://stackoverflow.com/questions/16150089/how-to-handle-COOKIEs-in-httpurlconnection-using-COOKIEmanager
* Get COOKIEs form COOKIEManager and load them to connection:
*/
if (msCOOKIEManager.getCOOKIEStore().getCOOKIEs().size() > 0) {
//While joining the COOKIEs, use ',' or ';' as needed. Most of the server are using ';'
con.setRequestProperty(COOKIE ,
TextUtils.join(";", msCOOKIEManager.getCOOKIEStore().getCOOKIEs()));
}
/*
* https://stackoverflow.com/questions/16150089/how-to-handle-COOKIEs-in-httpurlconnection-using-COOKIEmanager
* Get COOKIEs form response header and load them to COOKIEManager:
*/
Map> headerFields = con.getHeaderFields();
List COOKIEsHeader = headerFields.get(COOKIES_HEADER);
if (COOKIEsHeader != null) {
for (String COOKIE : COOKIEsHeader) {
msCOOKIEManager.getCOOKIEStore().add(null, HttpCOOKIE.parse(COOKIE).get(0));
}
}
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
public static void setResponseCode(int responseCode) {
WebService.responseCode = responseCode;
Log.i("Milad", "responseCode" + responseCode);
}
public static int getResponseCode() {
return responseCode;
}
}