【问题标题】:how to add content type for httppost using httpclient?如何使用 httpclient 为 httppost 添加内容类型?
【发布时间】:2013-05-29 08:17:03
【问题描述】:
  1. 这里我使用 httpclient 从 web 服务获取响应,使用 httppost 我发送请求,服务器数据内容类型是 application/x-www-form-urlencoded。
  2. 如何添加,这是我的代码,意味着我只想要来自服务的“application/x-www-form-urlencoded”这种类型的数据

    String postUrl = url;
            HttpClient client = new DefaultHttpClient(); 
    
            HttpPost request = new HttpPost(postUrl);
    
    
        request.setHeader("Content-type", "application/x-www-form-urlencoded");
        request.setHeader("Expect", "100-continue");
    
            HttpResponse response = client.execute(request);
            System.out.println("ddddddddddddddddddddddd  "+response);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));
            String line;
            builder = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            Log.d("Response", builder.toString());
            resObj = builder.toString();
            System.out.println("ddddddddddddddddddddddd  "+resObj);
        } catch (Exception e) {
            // TODO: handle exception
            Log.d("ERROR", e.toString());
        }
    

【问题讨论】:

  • 我不明白这个问题。
  • 我使用 request.setHeader("Content-type", "application/x-www-form-urlencoded"); request.setHeader("期望", "100-继续");
  • 所以,我不明白你的问题是什么。您显然设置了标题,但询问如何添加标题?
  • 我添加了,但它不是 wotking
  • 我没有从服务中获取数据

标签: android http url-encoding


【解决方案1】:

我尝试使用带有名称对值的httpost,它不起作用,所以用其他方式得出结论

它工作得很好, 注意:我们需要 java 1.7 jre

    String param1 = "9880938687";
    String param2 = "value2";
    // ... 
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("mobile", param1));
    String query = null;
    try {
        query = String.format("mobile=%s&param2=%s", 
             URLEncoder.encode(param1, charset), 
             URLEncoder.encode(param2, charset));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    URLConnection connection = new URL(targetURL).openConnection();
    connection.setDoOutput(true); // Triggers POST.
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

    try (OutputStream output = connection.getOutputStream()) {
        output.write(query.getBytes(charset));
    } 

    //InputStream response = connection.getInputStream();
    // ..
    //Get Response  
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer(); 
    while((line = rd.readLine()) != null) {
      response.append(line);
      response.append('\r');
    }
    rd.close();
    System.out.println("***"+response.toString());
    return response.toString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    相关资源
    最近更新 更多