使用Apache HttpClient发送UrlEncodedForm数据并使用euc-jp编码。

huangapple go评论67阅读模式
英文:

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(&quot;http://httpbin.org/post&quot;);
List&lt;NameValuePair&gt; params = new ArrayList&lt;&gt;();
params.add(new BasicNameValuePair(&quot;Testing&quot;, &quot;雄大&quot;));
request.setEntity(new UrlEncodedFormEntity(params, forName(&quot;EUC-JP&quot;)));

答案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(&quot;https://putsreq.com/xxxxxxxxxxxxxxxxxxxx&quot;);
    List&lt;NameValuePair&gt; params = new ArrayList&lt;&gt;();
    params.add(new BasicNameValuePair(&quot;Testing&quot;, &quot;雄大&quot;));
    request.setEntity(new UrlEncodedFormEntity(params, Charset.forName(&quot;euc-jp&quot;)));
    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.

huangapple
  • 本文由 发表于 2020年8月9日 09:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63321715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定