"post request using http" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "post request using http" answered properly. Developers are finding an appropriate answer about post request using http related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like post request using http. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on post request using http.
post request using http
private HttpResultHelper httpPost(String urlStr, String user, String password, String data, ArrayList<String[]> headers, int timeOut) throws IOException
{
// Set url
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// If secure connection
if (urlStr.startsWith("https")) {
try {
SSLContext sc;
sc = SSLContext.getInstance("TLS");
sc.init(null, null, new java.security.SecureRandom());
((HttpsURLConnection)conn).setSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
Log.d(TAG, "Failed to construct SSL object", e);
}
}
// Use this if you need basic authentication
if ((user != null) && (password != null)) {
String userPass = user + ":" + password;
String basicAuth = "Basic " + Base64.encodeToString(userPass.getBytes(), Base64.DEFAULT);
conn.setRequestProperty("Authorization", basicAuth);
}
// Set Timeout and method
conn.setReadTimeout(timeOut);
conn.setConnectTimeout(timeOut);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
if (headers != null) {
for (int i = 0; i < headers.size(); i++) {
conn.setRequestProperty(headers.get(i)[0], headers.get(i)[1]);
}
}
if (data != null) {
conn.setFixedLengthStreamingMode(data.getBytes().length);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
os.close();
}
InputStream inputStream = null;
try
{
inputStream = conn.getInputStream();
}
catch(IOException exception)
{
inputStream = conn.getErrorStream();
}
HttpResultHelper result = new HttpResultHelper();
result.setStatusCode(conn.getResponseCode());
result.setResponse(inputStream);
return result;
}
Source: notes.iopush.net
All those coders who are working on the Whatever based application and are stuck on post request using http can get a collection of related answers to their query. Programmers need to enter their query on post request using http related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about post request using http for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of post request using http while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "post request using http". Visit this developer's friendly online web community, CodeProZone, and get your queries like post request using http resolved professionally and stay updated to the latest Whatever updates.