【问题标题】:Http Post With Body带正文的 Http 帖子
【发布时间】:2011-10-10 07:30:46
【问题描述】:

我在objective-c中发送了发送http post的方法,在正文中我放了一个字符串:

NSString *requestBody = [NSString stringWithFormat:@"mystring"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];

现在在 Android 中我想做同样的事情,我正在寻找一种方法来设置 http 帖子的正文。

【问题讨论】:

    标签: java android


    【解决方案1】:

    您可以使用 HttpClient 和 HttpPost 来构建和发送请求。

    HttpClient client= new DefaultHttpClient();
    HttpPost request = new HttpPost("www.example.com");
    
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("paramName", "paramValue"));
    
    request.setEntity(new UrlEncodedFormEntity(pairs ));
    HttpResponse resp = client.execute(request);
    

    【讨论】:

    • client.setEntity 应该是 request.setEntity
    • 赞成,因为这对我帮助很大。您可能想要更新 DefaultHttpClient 现在它已被弃用为 HttpClientBuilder.create.build()
    【解决方案2】:

    你可以使用这个 sn-p -

    HttpURLConnection urlConn;
    URL mUrl = new URL(url);
    urlConn = (HttpURLConnection) mUrl.openConnection();
    ...
    //query is your body
    urlConn.addRequestProperty("Content-Type", "application/" + "POST");
    if (query != null) {
    urlConn.setRequestProperty("Content-Length", Integer.toString(query.length()));
    urlConn.getOutputStream().write(query.getBytes("UTF8"));
    }
    

    【讨论】:

    • 以及如何将 urlconnection 连接到 http 帖子?
    • 我认为需要添加:urlConn.setDoOutput(true);
    • 你为什么用application/POST而不是application/JSON
    【解决方案3】:

    您可以使用HttpClientHttpPost 尝试类似的操作:

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("mystring", "value_of_my_string"));
    // etc...
    
    // Post data to the server
    HttpPost httppost = new HttpPost("http://...");
    httppost.setEntity(new UrlEncodedFormEntity(params));
    
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse httpResponse = httpclient.execute(httppost);
    

    【讨论】:

      【解决方案4】:

      您可以使用 HttpClient 和 HttpPost 将 json 字符串作为正文发送:

      public void post(String completeUrl, String body) {
          HttpClient httpClient = new DefaultHttpClient();
          HttpPost httpPost = new HttpPost(completeUrl);
          httpPost.setHeader("Content-type", "application/json");
          try {
              StringEntity stringEntity = new StringEntity(body);
              httpPost.getRequestLine();
              httpPost.setEntity(stringEntity);
      
              httpClient.execute(httpPost);
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
      

      Json 正文示例:

      {
        "param1": "value 1",
        "param2": 123,
        "testStudentArray": [
          {
            "name": "Test Name 1",
            "gpa": 3.5
          },
          {
            "name": "Test Name 2",
            "gpa": 3.8
          }
        ]
      }
      

      【讨论】:

        【解决方案5】:
         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        

        然后为每一对添加元素

         nameValuePairs.add(new BasicNameValuePair("yourReqVar", Value);
         nameValuePairs.add( ..... );
        

        然后使用HttpPost:

        HttpPost httppost = new HttpPost(URL);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        

        并使用HttpClientResponse 从服务器获取响应

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-18
          • 1970-01-01
          • 1970-01-01
          • 2016-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多