Http Post With Body

前端 未结 5 395
轻奢々
轻奢々 2020-12-13 15:36

i have sent method in objective-c of sending http post and in the body i put a string:

NSString *requestBody = [NSString stringWithFormat:@\"mystring\"];
NSM         


        
相关标签:
5条回答
  • 2020-12-13 16:08

    You could use this snippet -

    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"));
    }
    
    0 讨论(0)
  • 2020-12-13 16:24

    You can use HttpClient and HttpPost to build and send the request.

    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);
    
    0 讨论(0)
  • 2020-12-13 16:24

    You can use HttpClient and HttpPost to send a json string as body:

    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 body example:

    {
      "param1": "value 1",
      "param2": 123,
      "testStudentArray": [
        {
          "name": "Test Name 1",
          "gpa": 3.5
        },
        {
          "name": "Test Name 2",
          "gpa": 3.8
        }
      ]
    }
    
    0 讨论(0)
  • 2020-12-13 16:25
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    

    then add elements for each pair

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

    Then use the HttpPost:

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

    and use the HttpClient and Response to get the response from the server

    0 讨论(0)
  • 2020-12-13 16:28

    You can try something like this using HttpClient and HttpPost:

    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);
    
    0 讨论(0)
提交回复
热议问题