英文:
Send UrlEncodedForm data using euc-jp in Apache HttpClient
问题
我想知道如何使用 EUC-JP 编码发送表单数据。我尝试下面的编码,但仍然将日文文本发送为 "?" 和奇怪的字符。谢谢!
这是我当前的做法(不正常工作):
HttpPost request = new HttpPost("http://httpbin.org/post");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Testing", "雄大"));
request.setEntity(new UrlEncodedFormEntity(params, forName("EUC-JP")));
英文:
I am wondering how I can send form data using euc-jp encoding. My attempt at encoding below is still sending japanese text as ? and odd characters. Thank you!
This is how I am currently doing it (not working properly):
HttpPost request = new HttpPost("http://httpbin.org/post");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Testing", "雄大"));
request.setEntity(new UrlEncodedFormEntity(params, forName("EUC-JP")));
答案1
得分: 0
Your code seems good to me. httpbin.org
不似乎处理 EUC-JP 响应。 相反,您可以使用 putsreq.com
查看您的请求参数。
import java.util.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.methods.*;
import org.apache.http.NameValuePair;
import java.nio.charset.*;
import org.apache.http.impl.client.*;
import org.apache.http.client.*;
import org.apache.http.*;
import java.io.*;
class Main {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
// 通过自己创建新的 PutsReq URL
HttpPost request = new HttpPost("https://putsreq.com/xxxxxxxxxxxxxxxxxxxx");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Testing", "雄大"));
request.setEntity(new UrlEncodedFormEntity(params, Charset.forName("euc-jp")));
HttpResponse response = httpclient.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
while ((reader.readLine()) != null) {
System.out.println (reader.readLine());
}
reader.close();
}
}
然后您将在检查页面中看到
Testing=%CD%BA%C2%E7
0xCDBA
表示 EUC-JP 中的 雄
。
英文:
Your code seems good to me. httpbin.org
doesn't seem to be handle EUC-JP in response. Instead you can use putsreq.com
to see your request parameters.
import java.util.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.methods.*;
import org.apache.http.NameValuePair;
import java.nio.charset.*;
import org.apache.http.impl.client.*;
import org.apache.http.client.*;
import org.apache.http.*;
import java.io.*;
class Main {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
// Create new PutsReq URL by yourself
HttpPost request = new HttpPost("https://putsreq.com/xxxxxxxxxxxxxxxxxxxx");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Testing", "雄大"));
request.setEntity(new UrlEncodedFormEntity(params, Charset.forName("euc-jp")));
HttpResponse response = httpclient.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
while ((reader.readLine()) != null) {
System.out.println (reader.readLine());
}
reader.close();
}
}
And you will see
Testing=%CD%BA%C2%E7
in the inspect page. 0xCDBA
means 雄
in EUC-JP.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论