user3153031
user3153031

Reputation: 3

send just a string with http Post but error

Sorry, I don't speak English very well.

I try to send string "Nhập nội dung bình luận để gửi đi!".

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(link);
String str ="Nhập nội dung bình luận để gửi đi!";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("file", str));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);

But I get "Nhập nội dung bình l" on my web server. How can I resolve it.

EDIT:

I encoded to UTF-8 by this code:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

replace by:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

but it still doesn't work.

Upvotes: 0

Views: 91

Answers (3)

Ted Bigham
Ted Bigham

Reputation: 4349

Try this change:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

The server must also be able to handle UTF-8 for that to work.

Alternatively, if the text will be rendered as HTML, you can encode the whole thing so the server doesn't need to support UTF-8.

String str ="Nh&#7853;p n&#7897;i dung bình lu&#7853;n &#273;&#7875; g&#7917;i &#273;i!";

That's html encoding, not URL encoding. Unfortunately Java doesn't include an html encoder as far as I know. You'd have to use a 3rd party library.

This thread lists a few libraries: Is there a JDK class to do HTML encoding (but not URL encoding)?

Full hack using an example from that other thread. Try something like this...

public void post() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(link);
    String str ="Nhập nội dung bình luận để gửi đi!";
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("file", encodeHTML(str)));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
}
public static String encodeHTML(String s)
{
    StringBuffer out = new StringBuffer();
    for(int i=0; i<s.length(); i++)
    {
        char c = s.charAt(i);
        if(c > 127 || c=='"' || c=='<' || c=='>')
        {
           out.append("&#"+(int)c+";");
        }
        else
        {
            out.append(c);
        }
    }
    return out.toString();
}

Upvotes: 1

Keerthivasan
Keerthivasan

Reputation: 12890

You need to encode the String with appropriate charset before sending to the web server. Otherwise, the String will be encoded in the default charset of the platform used. Check whether both server and program are using the correct charset to write and read the String

Say for example, it is UTF-8, Create String from ByteArray encoded with UTF-8 charset with code below

new String(YOUR_BYTE_ARRAY, Charset.forName("UTF-8"));

Reading the encoded String into ByteArray with same charset UTF-8 on the server side can be done as follows

YOUR_UTF8_STRING.getBytes("UTF-8");

Hope this clarifies!

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39385

Encode your url when sending from application:

// import java.net.URLEncoder;
str = URLEncoder.encode(str, "UTF-8");

And from the receiver decode the string:

// import java.net.URLDecoder;
str = URLDecoder.decode(str, "UTF-8");

Upvotes: 0

Related Questions