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);
Related
I am using HttpURLConnection class to send Post request to my server but its not working, I did the same Post call using DefaultHttpClient & its work properly, but in android org.apache.http.impl.client.DefaultHttpClient is deprecated, that's why I am using HttpURLConnection class. I don't know is anything wrong with the way I am sending the JSON object or something else.
Below is my code implementation
URL url = new URL(linkPath);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(10000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
writer.close();
out.close();
urlConnection.disconnect();
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.
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();
I am trying to hit this services but this gives me html reponse please help me where i am wrong
any help is really very help ful
HttpURLConnection connection;
java.net.URL u;
u = new java.net.URL("http://sdservices.iyogi.net/iyogi/webservicesnonrestv5/toolbarwebservices.asmx");
URLConnection uc = u.openConnection();
connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("SOAPAction", "http://sdservices.iyogi.net/SD/ToolbarService.svc/GetProductSettings?SubscriptionCode=W101982488");
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
InputStream is = connection.getInputStream();
inputStreamToString(is);
gives HTML in response:(
I need help with sending http get request. Like this:
URL connectURL;
connectURL = new URL(address);
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// do some setup
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
// connect and flush the request out
conn.connect();
conn.getOutputStream().flush();
// now fetch the results
String response = getResponse(conn);
et.setText(response);
I searched the web but any method I try, the code fails at 'conn.connect();'. Any clues?
Very hard to tell without the actual error message. Random thought: did you add the internet permission to you manifest?
<uses-permission android:name="android.permission.INTERNET"/>
If you want some demo code then try following:
URL url = new URL("url.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
and this:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
writeStream(out);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
Hope this helps.