2015-11-02 54 views
0

我有一個C#控制檯程序。我只想將JSON數據發送到POST RESTful服務。我應該遵循哪種方法?如何從C#發送JSON數據作爲參數傳遞給新澤西州的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

我解決了這個問題.........

C#代碼......發送JSON數據到web服務(後)......

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

     } 
相關問題