List of usage examples for javax.net.ssl HttpsURLConnection getHeaderField
public String getHeaderField(int n)
From source file:com.example.android.networkconnect.MainActivity.java
private String https_test(String urlString) throws IOException { String token = ""; URL url = new URL(urlString); Log.i(TAG, "Protocol: " + url.getProtocol().toString()); // if (url.getProtocol().toLowerCase().equals("https")) { trustAllHosts();/*from w w w. j av a 2 s . c o m*/ HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setReadTimeout(20000 /* milliseconds */); conn.setConnectTimeout(25000 /* milliseconds */); // conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setDoOutput(true); conn.setChunkedStreamingMode(0); conn.setRequestProperty("User-Agent", "e-venement-app/"); // OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); //writer.getEncoding(); //writer.write("&signin[username]=antoine"); //writer.write("&signin[password]=android2015@"); //writer.write("?control[id]="); //writer.write("&control[ticket_id]=2222"); //writer.write("&control[checkpoint_id]=1"); // writer.write("&control[comment]="); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); //On cre la liste qui contiendra tous nos paramtres //Et on y rajoute nos paramtres nameValuePairs.add(new BasicNameValuePair("signin[username]", "antoine")); nameValuePairs.add(new BasicNameValuePair("signin[password]", "android2015@")); //nameValuePairs.add(new BasicNameValuePair("control[id]", "")); //nameValuePairs.add(new BasicNameValuePair("control[ticket_id]", "2222")); //nameValuePairs.add(new BasicNameValuePair("control[checkpoint_id]", "1")); //nameValuePairs.add(new BasicNameValuePair("control[comment]", "")); OutputStream os = conn.getOutputStream(); BufferedWriter writer2 = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer2.write(getQuery(nameValuePairs)); writer2.flush(); //writer2.close(); //os.close(); // conn.setEntity(new UrlEncodedFormEntity(nameValuePairs)); //writer.write("&signin[_csrf_token]="+CSRFTOKEN); //writer.flush(); conn.connect(); String headerName = null; for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) { //data=data+"Header Nme : " + headerName; //data=data+conn.getHeaderField(i); // Log.i (TAG,headerName); Log.i(TAG, headerName + ": " + conn.getHeaderField(i)); } int responseCode = conn.getResponseCode(); if (responseCode == conn.HTTP_OK) { final String COOKIES_HEADER = "Set-Cookie"; cookie = conn.getHeaderField(COOKIES_HEADER); } if (conn.getInputStream() != null) { // token =getStringFromInputStream(conn.getInputStream()); Log.i(TAG, readIt(conn.getInputStream(), 15000)); token = readIt(conn.getInputStream(), 15000); Log.i(TAG, getStringFromInputStream(conn.getInputStream())); //data=readIt(conn.getInputStream(),7500); //Log.i(TAG,token); //token=readIt(conn.getInputStream(),7500); } //conn.connect(); // List<String> cookiesList = conn.getHeaderFields().get("Set-Cookie"); // } //conn.disconnect(); return token; }
From source file:com.kimbrelk.da.oauth2.OAuth2.java
protected final JSONObject requestJSON(Verb verb, String url, String postData) { try {//from w w w . j a va2s .c om mLog.append("SEND:\n"); HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", mUserAgent); connection.setRequestProperty("dA-minor-version", "" + VERSION.getMinor()); connection.setReadTimeout(30000); connection.setConnectTimeout(30000); mLog.append(verb.toString() + " "); mLog.append(url); mLog.append("\n"); connection.setRequestMethod(verb.toString()); if (verb == Verb.POST) { mLog.append(postData); mLog.append("\n"); connection.setDoOutput(true); connection.setDoInput(true); //connection.setRequestProperty("Authorization", "Basic " + base64(CLIENT_ID + ":" + loadLast())); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + postData.length()); connection.connect(); OutputStream os = connection.getOutputStream(); os.write(postData.getBytes()); os.flush(); } else if (verb == Verb.GET) { connection.setDoOutput(false); connection.setDoInput(true); connection.connect(); } mLog.append("\nRECV:\n"); try { InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader in = new BufferedReader(isr); String line = ""; String page = ""; while ((line = in.readLine()) != null) { page += line; } mLog.append(page); mLog.append("\n\n"); return new JSONObject(page); } catch (Exception e) { try { JSONObject json = new JSONObject(); json.put("status", "error"); try { InputStream is = connection.getErrorStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader in = new BufferedReader(isr); String line = ""; String page = ""; while ((line = in.readLine()) != null) { page += line; } mLog.append(page); mLog.append("\n\n"); return new JSONObject(page); } catch (Exception err) { } try { if (connection.getResponseCode() == 403 || connection.getResponseCode() == 429) { json.put("error_description", RespError.RATE_LIMIT.getDescription()); json.put("error", RespError.RATE_LIMIT.getType()); return json; } } catch (IOException er) { } String str = ""; str += "URL: " + url.split("[?]+")[0] + "\n"; //str += "POST Data: " + postData + "\n"; str += "\n"; for (int a = 0; a < connection.getHeaderFields().size() - 1; a++) { str += connection.getHeaderFieldKey(a) + ": " + connection.getHeaderField(a) + "\n"; } json.put("error_description", str); json.put("error", RespError.REQUEST_FAILED.getType()); return json; } catch (Exception er) { throw e; } } finally { connection.disconnect(); } } catch (Exception e) { try { e.printStackTrace(); JSONObject json = new JSONObject(); json.put("status", "error"); json.put("error", RespError.REQUEST_FAILED.getType()); json.put("error_description", RespError.REQUEST_FAILED.getDescription() + " : " + e); return json; } catch (JSONException er) { er.printStackTrace(); return null; } } }