In my android application, after a post request I am getting cookie like this
HttpResponse httpResponse = httpClient.execute(httpPost);
String cookie = httpResponse.getFirstHeader("Set-Cookie").getValue();
when I print the value of cookie in LogCat its value is PHPSESSID=150edfn1mmr4grmip6pd4h5pv6; path=/
However, when I send post request via a tool or online site like hurl.it the returned cookie is PHPSESSID=150edfn1mmr4grmip6pd4h5pv6; path=/, abtesting=0
Why httpResponse.getFirstHeader("Set-Cookie").getValue() returns lack of data? Thanks in advance
As like many other Java HttpClient based implementations. This can be due to the processing of the headers since based on Set-Cookie specifications abtesting=0 is an invalid attribute.
Although WikiPedia states The value of a cookie may consist of any printable ascii character (! through ~, unicode \u0021 through \u007E) excluding , and ; and excluding whitespace. The name of the cookie also excludes = as that is the delimiter between the name and value. The cookie standard RFC2965 is more limiting but not implemented by browsers. The key is RFC2965.
See the documentation associated with HttpCookie it links to RFC documents which have specific syntax/values that must be followed.
Related
I can use OkhttpClient to send a GET request to the server and get the cookie in the final response properly(the response code of which is 200 )and the cookie of which can be retrieved using addInterceptor().
Request-> 302(Set-cookie?) -> 302(Set-cookie?) -> 302(Set-cookie?) ->200(Set-cookie√(can be get using interceptor))
(the question mark ? means don't know how to get)
But the situation here is that there might be a series of redirects before the final response(200) is returned, the redirects carry the Set-cookie header. Is there any way using Okhttp to retrieve the cookies in the Set-cookie header in every redirect?
I have a string like "key=value" which I want to provide in my REST POST requests body. e.g.:
String content = "key=value";
Once set and trasmitted my logout output tells me, that my '=' sign content was converted to a '\u003d' wihch results in "key\u003dvalue" in my REST POST request content. Then the server responds a 400 Bad Request..
How can I prevent the '=' being converted to that unicode ?
Try this :
String content = "key\=value";
Goal
I am writing a program where a user's input is taken as a parameter and queried against an online API.
Problem
Oddly, I cannot get my parameter into my API successfully. The error I get is
"Could not look up user information; You have an error in your SQL syntax;" Which as it says plainly , is an SQL error. Therefore I was thinking there was a problem in passing my parameter since the application works when I hard code parameter and say "select name from table where id=1".
This is the parameter code and despite many edits and changes I got the same issue which caused me to look to my php even if everything works right in the browser.
HttpParams param = new BasicHttpParams();
ArrayList<NameValuePair> inputArguments = new ArrayList<NameValuePair>();
inputArguments.add(new BasicNameValuePair("id", idnum));
HttpClient client = new DefaultHttpClient(param);
HttpPost request = new HttpPost("http://myurl.com/DAIIS/getName.php");
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setEntity(new UrlEncodedFormEntity(inputArguments, "UTF-8"));
HttpResponse httpResponse = (HttpResponse) client.execute(request);
Where I think the problem lie
I belives the problem lies in my select statement
<?php
header("Content-Type:application/json");
//Connect to DB
include ("dbcon.php");
//Run query
$para=$_GET['id'];
$sql=("SELECT name FROM class where stu_id=$para");
I say this because after stripping my API to the bare minimum the program's error was Could not look up user information; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
but if i hard code the parameter (it works) or put something random like stu_id=$_GET['id']; it returns blank.
So is the way that I used this parameter incorrect for android? even if it works in the browser?
Thank you
As you asked for :
Just change '$_GET' to '$_POST',
As a side note
You can also check 'POST' request in browser, in order to do that add 'Rest client plugin' to your browser and you are done and have fun with api calls :)
I am new t ajax, but quite familiar with android. I am converting a ajax program to android app. As a part of it, i need to post data to the server. Below is the given post command in ajax.
var postTo = 'xyz.php';
$.post(postTo,{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' } ,
function(data) {
if(data.success){
window.localStorage["sa_id"] = data.mid;
window.location="getempdb.html";
}
if(data.message) {
$('#output').html(data.message);
} else {
$('#output').html('Could not connect');
}
},'json');
I want to implement this in android but under very little from the above statements. Could anyone who is good at ajax help me out with this thing. As of now, i get the user name and telephone number as a edit text input. I need to send this to php using http client. I know how to send data using php, but do not know what format to send and whether its a string to send or as a json object to send. Please help in interpreting the above code and oblige.
Apparently, this uses UrlEncodedFormEntity if you are using HttpClient in android.
This is created by using a List of NameValuePair.
from the parameters to the $.post:
{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' }
You have to create a NameValuePair for employee_name, one for phone ... each of which is fetched from a HTML element name employee_name, phone ... This is where you put the values from your EditTexts.
It returns a JSON formatted String, which you have to parse (typically using JSONObject obj = new JSONObject(result); once you have fetched the result from the server)
In this JSON object, you have a key named success, which format is not specified, except you can assume things went well if it is present ; a key mid, and a key message.
On an Android device, could an HttpURLConnection recognize the charset of its response automatically?
That is, if I have received some plain text response through an HttpURLConnection, can I get a right String (or maybe a right Reader) without knowing the charset used to encode the response?
You can call getContentType() which returns the responses MIME type. If it's a text-based response then this may include the character set, which you can then extract and pass to an InputStreamReader along with the InputStream you get by calling getInputStream().
If the response is not text-based, i.e. it's binary data, then the concept of charset is meaningless.