我有一个C#控制台程序。我只想将JSON数据发送到POST RESTful服务。我应该采用哪种方法?
@Path("/SetInfo")
public class SetInfo {
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public String AuthMySQL(String json) {
System.out.println("The JAX-RS runtime automatically stored my JSON request data: " + json);
return "";
}
答案 0 :(得分:0)
我解决了这个问题.........
C#代码......用于将JSON数据发送到webservice(post).....
var webAddr = "http://localhost:8080/TestWebservice/rest/SetInfo";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"Name\":\"MR.X\",\"ID\":\"AH1J4\"}";
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.Write(result);
}