Im trying to send and recieve data from web server with Post method in android .
this is my url call method :
InputStream input = null;
OutputStream output = null;
DataOutputStream printout;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(180000);
connection.setConnectTimeout(180000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("POST");
connection.connect();
JSONObject jsonParam = new JSONObject();
jsonParam.put("IMEI",IMEI);
PackageManager manager = this.context.getPackageManager();
PackageInfo info = null;
try {
info = manager.getPackageInfo(
this.context.getPackageName(), 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String version = info.versionName;
jsonParam.put("Version",version);
byte[] data_ = null;
try {
data_ = jsonParam.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String base64 = Base64.encodeToString(data_, Base64.DEFAULT);
printout = new DataOutputStream(connection.getOutputStream ());
printout.writeBytes(base64);
printout.flush ();
printout.close ();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
String APK_PATH = Environment.getExternalStorageDirectory() + "/download/";
File dir = new File (APK_PATH);
dir.mkdirs();
File _file = new File(dir, "mynew.apk");
boolean deleted = _file.delete();
output = new FileOutputStream(_file);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
and it returns 417 exception from server.
im using webapi + squid proxy. also i put this line in my webconfig :
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
</system.net>
but it didnt help.
in SO someone fixed the problem with this :
HttpClient httpclient = new DefaultHttpClient();
HttpParams params = httpclient.getParams();
params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,false);
but how can i use HttpParams inside HttpURLConnection ?!