利用jersey發送post請求

依賴java

springboot的版本是1.5.10spring

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>springboot

 

代碼實現

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.example.demo.User;

public static String post(String uri ,Form form) {
		Response response = null;
		try {
			Client client = ClientBuilder.newClient();
			WebTarget target = client.target(serverURI);
			response = target.request().buildPost(Entity.form(form)).invoke();
			String result = response.readEntity(String.class);
			System.out.println("result:"+result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {
			if(response!= null) {
				response.close();
			}			
		}
	}
相關文章
相關標籤/搜索