I'm trying to use HTTP POST to submit user information on a University login page. To do so I run the postData() method below. The code posts the credentials and reads the response. Below the code I have shown what the response status line and response entity show. (I edited the username and password for security ;) ).
It appears to execute since I get a response from the website, but I don't know how to interpret what it says or what I need to do to have a successful login. The website in question has a username field (id = user), password field (id = pass), and login button (id = submit). It is a secure website (https), but for now I'm not doing a SSL connection as I'm just trying to get this code working.
The immediate questions I have are: is it possible I need the SSL connection to make it work, and is Java an issue as mentioned in the response?
A little more background... This is done in a service, and it's the only task. If Java is the issue, how would I enable it without a webview?
public void postData() {
// Create a new HttpClient and Post Header
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httppost = new HttpPost("https://wiscmail.wisc.edu/login/");
TextView info = (TextView) findViewById(R.id.info);
EditText user = (EditText) findViewById(R.id.user);
EditText pass = (EditText) findViewById(R.id.pass);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", user.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("pass", pass.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.v(TAG, response.getStatusLine().toString());
info.setText(response.getStatusLine().toString());
HttpEntity responseEntity = response.getEntity();
Log.v(TAG, responseEntity.toString());
String response_string = inputStreamToString(response.getEntity().getContent()).toString();
Log.v(TAG, response_string);
} catch (ClientProtocolException e) {
Log.v(TAG, "client protocol exception");
info.setText("client protocol exception");
} catch (IOException e) {
Log.v(TAG, "IO exception");
info.setText("IO exception");
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
response.getStatusLine
HTTP/1.1 200 OK
response.getEntity().getContent()
<html><head></head><body onLoad="document.relay.submit()">
<form method=post action="https://login.wisc.edu/?appurl=wiscmail.wisc.edu/login"
name=relay><input type=hidden name=pubcookie_g_req
value="b25lPXdpc2NtYWlsLndpc2MuZWR1JnR3bz1XaXNjTWFpbCtMb2dpbiZ0aHJlZT0xJmZvdXI9YTUmZml2ZT1QT1NUJnNpeD13aXNjbWFpbC53aXNjLmVkdSZzZXZlbj1MMnh2WjJsdUx3PT0mZWlnaHQ9Jmhvc3RuYW1lPXdpc2NtYWlsLndpc2MuZWR1Jm5pbmU9MSZmaWxlPSZyZWZlcmVyPShudWxsKSZzZXNzX3JlPTAmcHJlX3Nlc3NfdG9rPTM5NjgzODc5MSZmbGFnPTA=">
<input type=hidden name=post_stuff value="user=username&pass=password">
<input type=hidden name=relay_url value="https://wiscmail.wisc.edu/PubCookie.reply">
<noscript><p align=center>You do not have Javascript turned on, please click the
button to continue.<p align=center><input type=submit name=go value=Continue>
</noscript></form></html>
EDIT:
Per suggestion I tried adding a setHeader to the http client, but I got the same response form the website.
httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
httppost.setHeader("Accept", "application/x-www-form-urlencoded");
Related
I'm coding a login system which will keep the user permanently logged on (until username or password is incorrect) but I'm having an issue with cookie storage. What I want to do is have the cookies store in local storage (probably shared preference). Though I have no idea where to start. This is my main HTTP Post function.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Add your data
httppost.setHeader("X-Requested-With", "XMLHttpRequest");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
StringBuffer sb = new StringBuffer();
BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
return sb.toString();
} catch (Exception e) {
//TODO: WIP
e.printStackTrace();
}
I want to set the cookies first (of course if there are any) then I would like to resave them after the httppost has executed. Where can I go about doing this?
Edit there is about 4 cookies that are saved.
This should help you get started:
http://docs.oracle.com/javase/tutorial/networking/cookies/definition.html
Check out this post as well:
https://stackoverflow.com/a/3587332/2495131
So I am trying to post to a rails app from an Android app I am writing. I am able to post successful from inside the rails app. I was also able to post successfully using a chrome add on called Simple Rest client.
When I try and post from the Android app its hitting the rails app but creating an empty post. None of the input data is being received by rails.
I read that 3rd party applications are only able to GET from a Rails app depending on authentication so to make sure this wasn't the issue I was having I added this to my Rails config.
# de-activate tolken auth
config.action_controller.allow_forgery_protection = false
At this point I am unsure as to where my issue lies, with my Rails backend or my Android client.
ok so the Rails post method in my controller that I'm trying to reach is here
# POST /orders
# POST /orders.json
def create
#order = Order.new(params[:order])
respond_to do |format|
if #order.save
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render json: #order, status: :created, location: #order }
else
format.html { render action: "new" }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
Here is the Android java code for sending the Post Request.
This is the method passing in the User input data I am trying to POST
private void postInformationtoAPI() {
showToast("POSTING ORDER");
List<NameValuePair> apiParams = new ArrayList<NameValuePair>();
apiParams.add(new BasicNameValuePair("drinks_id", GlobalDrinkSelected));
apiParams.add(new BasicNameValuePair("name", GlobalEditTextInputName));
apiParams.add(new BasicNameValuePair("paid" , GlobalIsPaid));
bgtPost = new BackGroundTaskPost(MAP_API_URL_POST_ORDER, "POST", apiParams);
bgtPost.execute();
goToOrderCompleted();
}
And this is the class that it is passed to, permorming the HTTP POST.
public class BackGroundTaskPost extends AsyncTask<String, String, JSONObject> {
List<NameValuePair> postparams = new ArrayList<NameValuePair>();
String URL = null;
String method = null;
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public BackGroundTaskPost(String url, String method, List<NameValuePair> params) {
this.URL = url;
this.postparams = params;
this.method = method;
for (int i = 0; i < postparams.size(); i++){
String test = postparams.get(i).toString();
Log.d("This is in the lisht:", test);
}
}
#Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
// Making HTTP request
try {
// Making HTTP request
// check for request method
if (method.equals("POST")) {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
httpPost.setEntity(new UrlEncodedFormEntity(postparams, HTTP.UTF_8));
Log.i("postparams : ", postparams.toString());
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils
.format(postparams, "utf-8");
URL += "?" + paramString;
HttpGet httpGet = new HttpGet(URL);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
Log.i("Logging out *is* before beffered reader", is.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
Log.i("Logging out *is* after beffered reader", is.toString());
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.i("json: ",json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data TEST " + e.toString());
}
// return JSON String
return jObj;
}
}
This is the what log's out for postparams in the above class, so I know data is actually being sent
04-03 21:36:23.994: I/postparams :(690): [drinks_id=41, name=Dave, paid=True]
This is what Log Cat is showing as a response from the server
04-03 20:56:08.247: I/json:(690): {"created_at":"2013-04-03T20:56:06Z","drinks_id":null,"id":1351,"name":null,"paid":null,"served":null,"updated_at":"2013-04-03T20:56:06Z"}
I am really struggling to understand where the issue lies with this and have been stuck on it for quite awhile. Any insight would be much appreciated. And if any more information is needed just shout.
Edit: logs from server
This is a successful post from the simple REST client
2013-04-03T23:13:31+00:00 app[web.1]: Completed 200 OK in 15ms (Views: 8.7ms | ActiveRecord: 5.2ms)
2013-04-03T23:13:42+00:00 app[web.1]: Started POST "/orders.json" for 89.101.112.167 at 2013-04-03 23:13:42 +0000
2013-04-03T23:13:42+00:00 app[web.1]: Processing by OrdersController#create as JSON
2013-04-03T23:13:42+00:00 app[web.1]: Parameters: {"updated_at"=>nil, "drinks_id"=>51, "id"=>1021, "name"=>"Test", "paid"=>true, "served"=>nil, "created_at"=>nil, "order"=>{"drinks_id"=>51, "name"=>"Test", "paid"=>true, "served"=>nil}}
2013-04-03T23:13:43+00:00 heroku[router]: at=info method=POST path=/orders.json host=fyp-coffeeshop.herokuapp.com fwd="89.101.112.167" dyno=web.1 connect=1ms service=25ms status=201 bytes=138
2013-04-03T23:13:43+00:00 app[web.1]: Completed 201 Created in 15ms (Views: 0.6ms | ActiveRecord: 13.2ms)
This is from the android app posting
2013-04-03T22:56:45+00:00 app[web.1]: Started POST "/orders.json" for 89.101.112.167 at 2013-04-03 22:56:45 +0000
2013-04-03T22:56:45+00:00 app[web.1]: Processing by OrdersController#create as JSON
2013-04-03T22:56:45+00:00 app[web.1]: Completed 201 Created in 23ms (Views: 2.2ms | ActiveRecord: 16.3ms)
2013-04-03T22:56:45+00:00 heroku[router]: at=info method=POST path=/orders.json host=fyp-coffeeshop.herokuapp.com fwd="89.101.112.167" dyno=web.1 connect=4ms service=37ms status=201 bytes=138
You're setting a content-type of JSON but not actually sending JSON, you're sending standard POST url-encoded parameters.
You need to actually send a JSON object:
JSONObject params = new JSONObject();
params.put("drinks_id", GlobalDrinkSelected);
params.put("name", GlobalEditTextInputName);
params.put("paid", GlobalIsPaid);
StringEntity entity = new StringEntity(params.toString());
httpPost.setEntity(entity);
The problem is that when you're building the POST in Android you're over-writing the entity (the body). You initially set it and then you set it again, effectively clearing out what you already set.
This is correct:
httpPost.setEntity(new UrlEncodedFormEntity(postparams));
But then a couple of lines later you over-write it with:
httpPost.setEntity(new StringEntity("UTF-8"));
So ditch that 2nd setEntity() call.
You can achieve what you're trying to do - setting the POST body in UTF-8 by tweaking your code like:
httpPost.setEntity(new UrlEncodedFormEntity(postparams, HTTP.UTF_8));
There is a website in which there are several drop down boxes.I made an android app that pulls the values from the site. Now there is a search box in the website , in the website we can choose options from the box and press submit , then it gives the result based on the options selected. I need to do the same in my app.
Need help.Thanks
To post data to a website you have send a HTTP POST request to it. You can put the data which you want to send in an array and send it to the php script.
You have to figure out with which ID your String is send to the server. In my example it is your_1 and your_2. This is different to each website. All new browsers can read this out in a developer console or something.
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("your_1", "data 1"));
nameValuePairs.add(new BasicNameValuePair("your_2", "data 2"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
After you have send this you have to get the response which you can read out with a StringBuilder.
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
// Return full string
return total;
}
Now you have the response and you can emphasize your special text with RegEx. This is a little bit tricky but this will help you.
I am working on an android project. I am new in android programming. How can i send a HTTP post request from my project to google app engine? I searched and found this code for sending request from android but its not working. Following is the code i am using:
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/servleturl");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", userEmailStr));
nameValuePairs.add(new BasicNameValuePair("password", userPasswordStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
info.setText(response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Thanks for help in advance.
Here's the class that i use for http requests in java:
public class WSConnector {
final String baseUrl = "http://www.myURL.com/"; //this is the base url of your services
String realUrlWithParams="";
String realUrl="";
String params = "";
String charset = "UTF-8";
HttpURLConnection connection = null;
public WSConnector(String serviceName,String params,String charset){ //we create the connector with everything we need (params must be ready as value pairs, serviceName is the name of your service):
if (charset!=null){
this.charset = charset;
}
this.realUrlWithParams = baseUrl+serviceName+"?"+params;
this.realUrl = baseUrl+serviceName;
}
public String getResponse(){//getResponse will get your the entire response String
String result = "";
System.out.println("trying connection");
try {
connection = (HttpURLConnection) new URL(realUrlWithParams).openConnection();
//connection.setRequestProperty("Accept-Charset", charset);
int status = connection.getResponseCode();
System.out.println("status:"+status);
if (status==HttpURLConnection.HTTP_OK){
InputStream responseStream = connection.getInputStream();
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(responseStream));
for (String line; (line = reader.readLine()) != null;) {
System.out.println("line is:" +line);
result = result+line;
System.out.println("result is:"+result);
}
}
} catch (MalformedURLException e) {
System.out.println("ERROR IN CONNECTOR");
e.printStackTrace();
} catch (IOException e) {
System.out.println("ERROR IN CONNECTOR");
e.printStackTrace();
}
System.out.println("finished connection");
return result;
}
if wanting to know some more, visit this CW:
httpurlconnection
I didn't give permission to user of my app to use internet. For doing that we just need to add this line in AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
Use restlet (http://wiki.restlet.org/docs_2.0/13-restlet/21-restlet.html) on app engine to handle the http requests as they come in. This isn't typically how app engine is used, but it'll work for what you want.
In andoid, just do a normal http request (http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java) on the appropriate url.
You should be able to figure out how to setup the url from the restlet tutorial. Once you deploy to app engine, the url will be something like http://myapp.appspot.com/goodstuff
Good luck!
I have a login form currently taking login parameters and logging into a website using HTTP Post Request. I am unsure of the server type so that could be the problem. Once it takes the login credentials, it coverts the inputstream to a string (all the html) and sets that to a textview. Here's the login:
private void postLoginData() throws ClientProtocolException, IOException {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("loginurl"); // Changed for question.
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("sid", "username"));
nameValuePairs.add(new BasicNameValuePair("pin", "pass"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String finalres = inputStreamToString(response.getEntity().getContent()).toString();
tvStatus.setText(finalres);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
And here's the inputStreamToString()
private StringBuilder inputStreamToString(InputStream is) throws IOException {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
// Return full string
return total;
}
The problem is that it ALWAYS just returns the HTML for the login page. When a user fails login on the site, it has a little message to indicate so. Even if I add incorrect credentials, it doesn't display anything different. Likewise, if I add the correct login, it still shows me just the login page HTML.
To check for HTTP status. Do something like this
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//Do Something here.. I'm logged in.
} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
// Do Something here. Access Denied.
} else {
// IF BOTH CASES not found e.g (unknown host and etc.)
}
This will exactly works as you want to check for status. thanks
I guess the problem is similar to this question. Check it out, there are some solutions, which might work for you in solving it.