如何通过java.net.HttpURLConnection发送Post Data或Form数据来访问webservices

时间:2015-12-15 11:59:34

标签: java rest

如何将POST数据作为REST对象发送到JSON网络服务而非发送为String,如下例所示:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import com.google.gson.Gson;

public class PostCall {

	public static void main(String[] args) throws IOException {
		String url = "https://portal.gamesparks.net/rest/games/295581sUaPkF/mongo/preview/script.PlayerData/find";
		URL obj = new URL(url);
		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

		// add reuqest header
		con.setRequestMethod("POST");
		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
		con.setRequestProperty("Authorization", "Basic  aWFta2luZy5pbmRpYUBnbWFpbC5jb206a2luZ0AwMDc=");

		// Send post request
		con.setDoOutput(true);
		con.setDoInput(true);
		con.setRequestMethod("POST");

		Query query = new Query("566abd13e4b03618c423050a");
		Par par = new Par(query);
		Gson gson = new Gson();

		OutputStream os = con.getOutputStream();
		os.write(gson.toJson(par).getBytes());

		// DataOutputStream wr = new DataOutputStream(con.getOutputStream());
		// wr.writeBytes(urlParameters);
		os.flush();
		os.close();

		int responseCode = con.getResponseCode();
		System.out.println("\nSending 'POST' request to URL : " + url);
		// System.out.println("Post parameters : " + urlParameters);
		System.out.println("Response Code : " + responseCode);

		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuffer response = new StringBuffer();

		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();

		// print result
		System.out.println(response.toString());
	}

}

用于POST DATA的Java代码

public class Par {
	 Query query;

	public Par(Query query){
		super();
		this.query = query;
	}
	
}

class Query{
	String _id;
	public Query(String _id){
		super();
		this._id = _id;
	}
}

在上面的示例中,我不确定POST数据是否会转到服务器。

我正在添加图片,请参阅enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用Google Gson API: 上课:

class MyClass{

    private String id;

    private String first_name;

    //getter setter
}

然后在你的代码中

MyClass myClass = new MyClass();
myClass.setId("12312312");
myClass.setFirstName("MyName");
Gson gson = new Gson();
os.write(gson.toJson(myClass));

答案 1 :(得分:0)

你可以使用RestTemplate。

  1. 创建一个包含帖子对象的类
  2. 在服务器中部署项目
  3. 相应地调用URL

    public static void main(String[] args){
    RestTemplate restTemplate = new RestTemplate();
    ObjectMapper mapper = new ObjectMapper();
    JSONInput post = new JSONInput();  
    //create a JSONInput class with all your datas to be posted for URL
    //set the values here for ex: post.setString("test");
    String deployedURl = "http://localhost:portNo/projectName/restService"
    System.out.println(restTemplate.postforObject(deployedURl,post,OutputExpected.class));
    

    同样,如果它是一个get方法也一样(如果你将值附加到URL)

    String getInput = deployedURl+"?input=inputforTest"
    System.out.println(restTemplate.getforObject(getInput ,OutputExpected.class));
    
  4. }