"Java knowledge application" Java implements HTTP requests through Get and Post.

Http request, it is very common and data interactively.

The following explanation: two actual cases of Get and Post.

 

Action (controller) for testing.

@RequestMapping(value = "getData.json")
public @ResponseBody
ServerResponse getData(HttpSession session,@RequestBody People people){
    return new ServerResponse(people);
}

 

Case (post):

import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final String URL= "http://XXX.XXX.XXX.XXX:XXXX/getData.json";

    public static void main(String[] args) {
        String url = URL;
        Map map = new HashMap();
        map.put("name","李磊");
        map.put("age","18");
        JSONObject json = new JSONObject(map);
        JSONObject res = PostInterface.postJson(url,json);
        System.out.println(res.toString());
    }
}
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.nio.charset.Charset;

/**
 * 请求类
 */
public class PostInterface {

    public static JSONObject postJson(String url, JSONObject json) {
        HttpClient CloseableHttpClient=HttpClientBuilder.create () Build ();. 
        HttpPost POST = new new HttpPost (URL); 
        the JSONObject Response = null ;
         the try {
             // encoded using UTF-8, to avoid distortion Chinese 
            StringEntity = S new new StringEntity (json.toString (), the Charset .forName ( "GBK" )); 
            s.setContentType ( "file application / json"); // send data to set json contentType 
            post.setEntity (S); 

            the HttpResponse RES = httpclient.execute (POST);
             IF (res.getStatusLine () .getStatusCode () == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                response = new JSONObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
}

operation result:

 

Action (controller) for testing.

@ RequestMapping (value = "getData.json" )
 public @ResponseBody 
serverResponse getData (the HttpSession the session, String Flag) { 
    String str; 
    IF (! StringUtils.isBlank (Flag)) { 
        str = "Your biography is" "+ flag +" "" ; 
    } the else { 
        STR = "your pass is empty" ; 
    } 
    return  new new serverResponse (STR); 
}

 

Case (get):

public class Main {
    private static final String URL= "http://XXX.XXX.XXX.XXX:XXXX/getData.json";

    public static void main(String[] args) {
        String url = URL;
        String res = PostInterface.doGet(url+"?flag=1","utf-8");
        System.out.println(res.toString());
    }
}
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 * 请求类
 */
public class PostInterface {

    public static String doGet(String url,String charset){
        if(null == charset){
            charset = "utf-8";
        }
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpGet httpGet = null;
        String result = null;
        try {
            httpGet = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpGet);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

operation result:

 

If you encounter problems in the implementation process of the case: can be added to the group communication, or comment below

 

 

Guess you like

Origin www.cnblogs.com/jssj/p/11598651.html