I am sending the string value to the php file on the server like this:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("serverurl");
Log.d("httppost..", httppost + "");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", dataCount + ""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
But, I am not seeing any data which I have sent from the app.
Thanks for any help.
Related
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.
I try to send string "Привет мир!"
String link = POST_URL;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(link);
String xml ="Привет мир";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("file", xml));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
And save it by php script:
if(!empty($_REQUEST['file'])){
$fp = fopen("C:\\windows\\temp\\1.xml", "w");
$mytext =$_POST["file"];
$test = fwrite($fp, $mytext);
fclose($fp);
But I get ?????? ????? on my web server, I try reopen file with utf encoding, but it doesn't help. How can I resolve it.
The StringEntity's charset to UTF-8. These lines will do the trick:
httpPost.setEntity(new StringEntity(body, HTTP.UTF_8));
This worked for me:
HttpPost httpPost = new HttpPost("http://someurl.com");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));
I need to post my string to server having html content,
Here is my code
Url Builder
Uri.Builder builder = new Uri.Builder()
.scheme("http")
.authority(domain)
.path(pageName)
.appendQueryParameter("myaction", "dopost")
.appendQueryParameter("mesg", msg.replace("\n","<br />"));
And the generated url looks like
domainname/pagename?myaction=dopost&mesg=Hello%2C%3Cbr%20%2F%3E%3Cbr%20%2F%3ETesting
execute http code
HttpGet getRequest = new HttpGet(url);
getRequest.setHeader(CONTENT_TYPE, MIME_TEXT_HTML); // html/text, utf-8
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(getRequest);
but this will not posted getting forbidden error after executing
Thank in advance
maybe a silly mistake, but a post usually is done with an HttpPost instead of an HttpGet
and looks like this:
// 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("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}
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 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;
}