Send a "zip" file from an Android device to web service Rest - android

Client :
URL url =new URL("http://xxx.xxx.x.xx:8080/wre/zipFile"); // LocalHost
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "application/zip");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(50000);
BufferedOutputStream sout = new BufferedOutputStream(conn.getOutputStream());
FileInputStream bis = new FileInputStream(zipfile);
int i;
byte bytes[]=new byte[4096];
while((i=bis.read(bytes))!=-1){
sout.write(bytes,0,i);
sout.flush();}
sout.close();
bis.close();
is correct code ?
Server:
what is the server side code?
i.e. download the zip file (d: / zipFile /) and then send to the client by mail
thanks.

Related

Connection Failiure when trying to upload an image file

I am trying to upload an image by converting it to a string buffer to a server. But I always get the mentioned error exception when connecting.
isConnected failed: EHOSTUNREACH (No route to host)
Above is the exception I am getting
This is the code I have used to upload image
jsonSendData = new JSONObject();
jsonSendData.put("data", sendData);
URL url = new URL(getFieldTCTEnableStr("URL"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
//method
conn.setRequestMethod("POST");
//header
conn.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(jsonSendData.toString());
out.flush();
out.close();
conn.disconnect();

Android: Send a file as an array of bytes using HttpURLConnection

I want to send a file via a POST HTTP request but can't find how to handle it first...
I would do this with a string parameter:
String html_params = "myparam=myparam";
byte[] outputInBytes = html_params.getBytes("UTF-8");
URL url = new URL("http://www.myurl.com/senddata.asp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "text/html");
OutputStream os = conn.getOutputStream();
os.write(outputInBytes);
os.close();
response = conn.getResponseCode();
However I have no idea of how to transform and send a file as an array of bytes instead of a string...
Thanks for your help

FileNotFoundException for site with tilde when using httpurlconnection

There is a strange problem when trying post some data to site with tilde.
How could I fix that in Android?
For example, this is my site:
http://host-site.com/~username/form.php
Here is my code for that:
url = new URL(hostSiteString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Host", "myHost");
urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0");
urlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
urlConnection.setRequestProperty("Accept-Language", "en-US;q=0.7,en;q=0.3");
urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
urlConnection.setRequestProperty("Connection", "keep-alive");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setDoOutput(true);
BufferedOutputStream bos = new BufferedOutputStream(urlConnection.getOutputStream());
bos.write(readyToken.getBytes(), 0, readyToken.length());
bos.flush();
bos.close();
Found solution, I had to decode my url.
String decode = URLDecoder.decode(hostSiteString);
url = new URL(decode);

Problems doing a POST request from android

I'm trying to login in a Django server from an android app, from the web it works fine, but when I try to do it from the app I get an Internal Server Error. I'm using HttpURLConnection:
url = new URL(loginUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("GET");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.getContent();
conn.disconnect();
CookieStore cookieJar = cManager.getCookieStore();
List <HttpCookie> cookies = cookieJar.getCookies();
String csfr = null;
for (HttpCookie cookie: cookies) {
Log.d("cookie", ""+cookie);
if(cookie.getName()=="csrftoken"){
csfr = cookie.getValue();
break;
}
}
String postParams = "csrfmiddlewaretoken="+csfr+"&username="+user+"&password="+pass+"&this_is_the_login_form=1&next=";
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", ""+postParams.getBytes().length);
conn.setRequestProperty("User-Agent","Mozilla/5.0");
conn.setFixedLengthStreamingMode(postParams.getBytes().length);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(postParams);
dos.flush();
dos.close();
Log.d(conn.getResponseCode()+"", ""+conn.getResponseMessage());
The GET request works fine and fetch the csrf cookie just right, I don't know what I'm missing in the POST request. Here's a capture of the data posted from a browser request:
csrfmiddlewaretoken=zqvmoYTLeimB9RW5cMj5xTyLhIzR8kqr&username=user&password=123456&this_is_the_login_form=1&next=
edit:
Finally got it working, diasabled csfr protection, added some RequestProperty, read the response (getting pipe broken error en server side if not read) and added a '/' at the end of the URL. Porbably the causer of all or almost all of the error was the missing '/' on the url.
Final working coding:
url = new URL("http://XX.XXX.XXX.XXX/accounts/login/");
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("GET");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.getContent();
conn.disconnect();
/*CookieStore cookieJar = cManager.getCookieStore();
List <HttpCookie> cookies = cookieJar.getCookies();
String csfr = null;
for (HttpCookie cookie: cookies) {
if(cookie.getName()=="csrftoken"){
csfr = cookie.getValue();
break;
}
}*/
String postParams = "username=patient1&password=123456";
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Length", ""+postParams.getBytes().length);
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8");
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setFixedLengthStreamingMode(postParams.getBytes().length);
OutputStream os = conn.getOutputStream();
os.write(postParams.getBytes("UTF-8"));
InputStream is = conn.getInputStream();
while(is.read() > -1);
Log.d(conn.getResponseCode()+"", ""+conn.getResponseMessage());
You might need to disable csrf protection in your django login view.

Android upload file to HttpServer using HTTP POST (File type: httppostedfilebase)

I need to upload file to server using httppost type. In server side they received the file in httppostedfilebase instance.
How to do this? Suggest me.
try this:
url = new URL("http://....");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(stufftosend);
outputStream.flush();
outputStream.close();

Categories

Resources