blahNow I am doing an ANdroid application.In my app I have to login using a url.Just like...www.blah.com/api/login/username/password.
private void sendAccelerationData()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
nameValuePairs.add(new BasicNameValuePair("","test3"));
nameValuePairs.add(new BasicNameValuePair("","pass3"));
this.sendData(nameValuePairs);
}
private void sendData(ArrayList<NameValuePair> data)
{
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://blah.com/api/login/");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
}
}
but I am getting 404 error.and if i write my sendData() like
HttpPost("http://eesnap.com");
and sendAccelerationData() like
nameValuePairs.add(new BasicNameValuePair("","api"));
nameValuePairs.add(new BasicNameValuePair("","login"));
nameValuePairs.add(new BasicNameValuePair("","test3"));
nameValuePairs.add(new BasicNameValuePair("","pass3"));
I am getting 200 success.
If post www.blah.com/api/login/username/password on brower then I am getting a result on the browser
Try:
HttpPost("http://www.blah.com/api/login/");
I understand my error.In 'Get()' method i can write code like above but in post method I have to use name value pair.
Related
My problem is this: after the GET request authorization and save cookies trying to perform a POST request to add data, but the server responds with 500 code. What's funny, because if POST query string form in a browser, it is executed correctly. The code below.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(site + "/admin/users/login_do/?login=admin&password=demo");
HttpResponse response = httpclient.execute(httpget);
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
HttpPost httppost = new HttpPost(site + "/admin/news/add/5/item/do/");
httppost.addHeader("Cookie", "PHPSESSID="+cookies.get(0).getValue()+"; umicms_session="+cookies.get(1).getValue()+"; stat_id="+cookies.get(2).getValue());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("active", "1"));
nameValuePairs.add(new BasicNameValuePair("name", "test"));
nameValuePairs.add(new BasicNameValuePair("data[new][anons]", "anno"));
nameValuePairs.add(new BasicNameValuePair("data[new][content]", "cont"));
nameValuePairs.add(new BasicNameValuePair("data[new][publish_time]", "1420202020"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
response = httpclient.execute(httppost);
StatusLine status = response.getStatusLine();
Log.d("my",String.valueOf(status.getStatusCode()));
Log.d("my",String.valueOf(status.getReasonPhrase()));
I've tried to run POST using HttpURLConnection, but also received 500 response.
Who can tell what might be the problem?
Problem solved. I Forgot to send csrf token with POST request and set Referer header
nameValuePairs.add(new BasicNameValuePair("csrf", csrf));
httppost.addHeader("Referer", referlink);
I have a code to make an httpPost, but i can't log using the parameters in the array, but if i make all the complete url i will log with no problem !, what am i missing ?
String URL = "http://domain.com/projexct/form.aspx"; //won't work
// String URL = "http://domain.com/projexct/form.aspx?user=aa&pass=11";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", "aa"));
nameValuePairs.add(new BasicNameValuePair("pass", "11"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
EDIT1
It doesn't throw and error, but if i use the parames in namevaluePairs it shows just the login page, if i use the URL commented it will actualy log in.
In my application I'm using outpost method to send data to web service which is in .net.Now when i send string with spaces or special characters it takes the string with e.g. test string would display like test+string.
I'm using httpDefaultClient with nameValuePair to send data...i've used UrlEncode function to encode my string but still result is the same...
Please help me...
Here is my code
//web service call
HttpClient client=new DefaultHttpClient();
HttpPost request = new HttpPost();
request.setURI(new URI(url2));
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
// nameValuePairs.add(new BasicNameValuePair("CreateHotSpots", value))
nameValuePairs.add(new BasicNameValuePair("sKey",""+globalClass.getUser_key()));
nameValuePairs.add(new BasicNameValuePair("sLAKE",encodedlakename));
if(!(DragAndDropPinActivity.point.isEmpty())){
nameValuePairs.add(new BasicNameValuePair("sLAT",""+DragAndDropPinActivity.point.get(0)));
nameValuePairs.add(new BasicNameValuePair("sLon",""+DragAndDropPinActivity.point.get(1)));
}
else
{
url2=url2.concat("&sLAT="+""+myLatitude);
url2=url2.concat("&sLon="+""+myLongitude);
}
nameValuePairs.add(new BasicNameValuePair("sDesc",encodedDesc));
nameValuePairs.add(new BasicNameValuePair("sSpeciesofFish",encodedFishSpecies));
nameValuePairs.add(new BasicNameValuePair("sBaitUsed",encodedbUsed));
nameValuePairs.add(new BasicNameValuePair("sWeatherInformation",encodedwInfo));
nameValuePairs.add(new BasicNameValuePair("season",encodedlseason));
nameValuePairs.add(new BasicNameValuePair("sDesc",""+encodedDesc));
if(share)
{
nameValuePairs.add(new BasicNameValuePair("sShareInfo","true"));
}
else
{
nameValuePairs.add(new BasicNameValuePair("sShareInfo","false"));
}
Log.e("name value pairs",""+nameValuePairs.toString());
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
request.setEntity(entity_st);
HttpResponse response = client.execute(request);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Responce=EntityUtils.toString(resEntity);
Log.i("responce ======",""+Responce);
}
}
catch (Exception e) {
e.printStackTrace();
String message=e.getMessage();
Log.e("meaasge with erroe",message);
}
return Responce;
}
step 1
You have specified a "UTF-8" encoding in
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
Try to change it to
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs);
step 2
I noticed that the strings you are adding to the request are called encodeXXXX
Does this mean that you are encoding them before adding them to the ValuePairs?
If so, stop doing this and keep them as normal strings.
I'm performing login on server using HttpPost method.
CommonsConnection.initHttpConnection();
HttpResponse response;
String contentOfMyInputStream;
HttpGet initPostMethod = new HttpGet("https://stat.volia.com:8443/ktvinet/index.jsp");
response = CommonsConnection.hc.execute(initPostMethod);
HttpPost Statistics = new HttpPost("https://stat.volia.com:8443/ktvinet/j_security_check");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("j_username", "username"));
nameValuePairs.add(new BasicNameValuePair("j_password", "password"));
nameValuePairs.add(new BasicNameValuePair("submit", "%C2%EE%E9%F2%E8"));
Statistics.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = CommonsConnection.hc.execute(Statistics);
HttpGet mainPage = new HttpGet("https://stat.volia.com:8443/ktvinet/main.do");
response = CommonsConnection.hc.execute(mainPage);
contentOfMyInputStream = EntityUtils.toString(response.getEntity());
The response I get - body of webpage - is like 1/5 part of webpage, which ends with "...".
Am I missing some parameter for httpclient ?
How are you examining the response? if you're looking int he debugger or something similar, it may well truncate long strings, try dumping it in a file.
Logcat displays only part of long string response.
I am executing the following post in Android:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("account", "login"));
nameValuePairs.add(new BasicNameValuePair("email", "email#email.com));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
In the server side I return the cgi.request_method variable and it is GET.
Shouldn't it be POST? Am I missing something?
That code should initiate a POST, not a GET. I would guess that something on the server side is misconfigured.
My problem was send http instead of https