如何将JSON数据作为参数从C#发送到Jersey RESTful Service

时间:2015-11-02 03:04:21

标签: c# json web-services rest post

我有一个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 "";
    }

1 个答案:

答案 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);

        }