I am developing an Android app, in which I need to consume a web service. I am getting the 200 response when I check in SoapUI ( I am not able to attach my SoapUI screen shot). Basically I need to send the query parameters to a login web service which is a POST request.
I am trying the below code snippet in my Android file, but it is not working. Am I doing anything wrong here?
String loginUrl = "http://my-login-portal.in/LogonServlet";
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
nameValuePair.add(new BasicNameValuePair("page", "/OrderHandlingServlet?api=login"));
nameValuePair.add(new BasicNameValuePair("txtDomain", "MyDomain"));
nameValuePair.add(new BasicNameValuePair("txtUid", "user1"));
nameValuePair.add(new BasicNameValuePair("txtPwd", "pwd1"));
httppost = new HttpPost(loginUrl);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
httppost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
response = httpclient.execute(httppost);
use this code :
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
nameValuePair.add(new BasicNameValuePair("page", "/OrderHandlingServlet?api=login"));
nameValuePair.add(new BasicNameValuePair("txtDomain", "MyDomain"));
nameValuePair.add(new BasicNameValuePair("txtUid", "user1"));
nameValuePair.add(new BasicNameValuePair("txtPwd", "pwd1"));
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, INSERTCONNECTIONTIMEOUT);
HttpClient client = new DefaultHttpClient(params);
HttpPost request = new HttpPost(URL);//put url here
request.setEntity(new UrlEncodedFormEntity(nameValuePair));
BasicHttpResponse httpResponse = (BasicHttpResponse) client.execute(request);
String data = StreamToString(httpResponse.getEntity().getContent());
Related
I tried to send data to server with httpPost.
its look like this:
String username = URLEncoder.encode("myUsername","windows-1255");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("****");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username", username));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
But it's sending to the server char as %25XX and not %XX.
Why it's happening?
I was need to add the entity as string and not as list:
String param="username"+username;
post.setEntity(new StringEntity(param));
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 send request with this code:
final DefaultHttpClient client = new DefaultHttpClient();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("number", pNumber));
final HttpPost httpPost = new HttpPost(URL_MAIN);
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
client.execute(httpPost);
It sends request in UTF-8, but I need cp1251 (site works only with this CP). How to encode it into the cp1251?
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
I am trying to build a tumblr client in android but am running into response code 403 - Forbidden.
The api docs suggest i use email and password parameters to validate credentials and get account information.
I have tried out the following code but as specified get a 403 response code.
can anyone suggest what i m doing wrong.
String url = "http://www.tumblr.com/api/authenticate";
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 20000);
HttpConnectionParams.setSoTimeout(params, 20000);
DefaultHttpClient httpclient = new DefaultHttpClient(params);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(_username + ":" + _password);
httpclient.getCredentialsProvider().setCredentials(new AuthScope(null,-123),credentials);
HttpPost method = new HttpPost(url);
HttpResponse response = httpclient.execute(method);
response.getStatusLine().toString(); // this gives me a 403 res code (Forbidden)..
Here's what i use in my app to authenticate:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.tumblr.com/api/authenticate");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", Username));
nameValuePairs.add(new BasicNameValuePair("password", Password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() != 200) {
return false;
}
return true;
}