Java HTTP post request UTF-8
•
Java
My J2EE application can receive post requests from JSP pages, no problem
But if I send a post request using another Java application, the parameter I receive is not a UTF - 8 string
Here's my code:
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();
Did I do something wrong?
Thank you for your reply
Solution
This job!!!
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,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; } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码