JAVA Http POST请求UTF-8

时间:2013-09-16 08:20:21

标签: java http utf-8

我的J2EE应用程序能够从JSP页面接收POST请求,没问题。

但如果我使用另一个java应用程序发送POST请求,则收到的参数不是UTF-8字符串。

这里有我的代码:

URL url = new URL("http://localhost:8080/ITUNLPWebInterface/SimpleApi");
HttpURLConnection cox = (HttpURLConnection) url.openConnection();

cox.setDoInput(true);
cox.setDoOutput(true);
cox.setRequestMethod("POST");
cox.setRequestProperty("Accept-Charset", "UTF-8");
cox.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
cox.setRequestProperty("charset", "UTF-8");

DataOutputStream dos = new DataOutputStream(cox.getOutputStream());
String query = "tool=ner&input=şaşaşa";
dos.writeBytes(query);
dos.close();

我做错了吗?

感谢您的回复

6 个答案:

答案 0 :(得分:8)

这项工作!!!。

package com.erenerdogan.utils;

import com.erenerdogan.webservice.ServiceInterface;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author erenerdogan
 */
public class WebService 
{
private String server;

    public WebService(String server) {
        this.server = server;
    }

private HttpPost createPostRequest(String method, Map<String, String> paramPairs){
    // Creating HTTP Post
    HttpPost httpPost = new HttpPost(server + "/" + method);
    // Building post parameters
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(paramPairs.size());
    for (String key : paramPairs.keySet()){
        nameValuePair.add(new BasicNameValuePair(key, paramPairs.get(key)));
            System.out.println("Key : "+ key + " - Value : "+ paramPairs.get(key) );
    }

    // Url Encoding the POST parameters
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // writing error to Log
        e.printStackTrace();
    }
    return httpPost;
}

public String callServer(String method, Map<String, String> paramPairs) throws ClientProtocolException, IOException{

    // Creating HTTP client
    HttpClient httpClient = new DefaultHttpClient();

    HttpParams httpParameters = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 10 * 1000);
    HttpConnectionParams.setSoTimeout(httpParameters, 3 * 1000);
    HttpResponse httpResponse = httpClient.execute(createPostRequest(method, paramPairs));
    HttpEntity httpEntity = httpResponse.getEntity();
    String xml = EntityUtils.toString(httpEntity);

    return xml;
}
}

答案 1 :(得分:5)

DataOutputStream.writeBytes(String)的文档说

  

将字符串作为字节序列写入基础输出流。字符串中的每个字符按顺序写出,通过丢弃其高8位。如果没有抛出异常,则写入的计数器将增加s的长度。

而是使用cox.getOutputStream().write(query.getBytes("UTF-8"));

DataOutputStream在这里是多余的。

答案 2 :(得分:2)

试试这个

HttpClient client = new DefaultHttpClient();
HttpPost port = new HttpPost("http://localhost:8080/ITUNLPWebInterface/SimpleApi");

List<NameValuePair> parameters = new ArrayList<NameValuePair>(3);
parameters.add(new BasicNameValuePair("tool", "ner"));
parameters.add(new BasicNameValuePair("input", "şaşaşa"));
//post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
post.setEntity(new UrlEncodedFormEntity(params, "ISO-8859-3")); //try this one

HttpResponse resp = client.execute(post);

https://en.wikipedia.org/wiki/ISO/IEC_8859-3似乎支持你的特殊角色ş

答案 3 :(得分:1)

它适用于我:

connection = (HttpURLConnection) url.openConnection();
...
byte[] data = message.getBytes("UTF-8");
...
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.write(data);
wr.close();

答案 4 :(得分:0)

a)“application / x-www-form-urlencoded”没有charset参数;它基本上限于ASCII

b)要发送非ASCII字符,您需要以UTF-8(不是客户端的默认编码)百分比 - 对它们进行编码;有关详细信息,请参阅http://www.w3.org/TR/2014/REC-html5-20141028/forms.html#application/x-www-form-urlencoded-encoding-algorithm

答案 5 :(得分:0)

基于HttpClient的示例“ FluentRequests.java”和erenerdogan的答案:

Content content = Request.Post("http://localhost:8080/ITUNLPWebInterface/SimpleApi")
        .body(new UrlEncodedFormEntity(
                Form.form()
                .add("tool", "ner")
                .add("input", "şaşaşa")
                .build(), "UTF-8"))
        .execute().returnContent();
System.out.println(content);