有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用不带urlencode参数的Httppost的java Android post请求

这是我的密码。。。是否可以删除urlencode? 我希望传递post参数而不使用urlencode。 谢谢

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

共 (1) 个答案

  1. # 1 楼答案

    你可以试试这样的

    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
    BasicHttpEntity filter = new BasicHttpEntity();
    filter.setContent(new ByteArrayInputStream("Filter=id :EQUALS: 12345 :AND: stringdata :EQUALS: hi".getBytes("UTF-8")));
    httppost.setEntity(filter);
    

    谢谢