Android HttpPost - Send request in cp1251 - android

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?

Related

Android send json to server

i write this code for send json to asp.net web page:
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", myCountry.name);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json")
but in this line :
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
i get this error:
UrlEncodeFromEntity (java.util.List <? extends org.apache.http.NameValuePair>,String) in UrlEncodedFromEntity cannot be to (org.apache.http.entity.StringEntitym,String)
How can solve this?
Use httpPost.setEntity(new StringEntity(json.toString()));
instead of
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
Just try this line httpPost.setEntity(se);
-For arabic characters try Namevaluepairs
Example:-
ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();
nameValuePairs1.add(new BasicNameValuePair("name", myCountry.name));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
Thanks!

HttpPost encoding to windows-1255 add 25 to each char

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));

Sending the data to the server using HttpPost Request in android

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.

How to post string to server using BasicNameValuePair

I'm trying to post combination of string to my backend server. How can I achieve that using BasicNameValuePair. Here are some code which I was trying:
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpClient = new DefaultHttpClient(params);
HttpPost post = new HttpPost("API HERE");
List<NameValuePair> postData = new ArrayList<NameValuePair>();
postData.add(new BasicNameValuePair("username", "the_username"));
postData.add(new BasicNameValuePair("password", "the_password"));
I want to send username and password like:
username=USER&password=PWD
How to achieve the successful post to the server.
Help will be appreciated.
The following is what I use for all of my POST variables:
HttpParams httpParams = new BasicHttpParams();
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost;
httpPost = new HttpPost(Net_URL + Net_Get);
httpPost.setEntity(new UrlEncodedFormEntity(/*Name Value Pairs*/));
HttpResponse response = httpClient.execute(httpPost);
As a side suggestion, I would first do an some sort of encryption of a users password before sending it over the network, I have seen many who do not do this ^.^

How to post data having HTML content in query string

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);
}

Categories

Resources