2014-01-28 42 views
0

我正在从C#调用drupal Web服务。我在drupal中的web服务接受两个参数。当我使用Chrome扩展POSTMAN检查我的Web服务时。 IT工作正常,并返回数据。但我不知道如何在Web服务中传递这两个值。我写我的代码,这样如何将变量传递给c#中的Web服务?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://careernet.localhost/rep-details/report_details/retrieve"); 
        request.Method = "POST"; 
        request.ContentType = "application/json"; 
        request.Accept = "application/xml"; 
        request.CookieContainer = new CookieContainer();      
        request.CookieContainer.Add(new Uri("http://careernet.localhost/rep-details/report_details/retrieve"), new Cookie("sid", sessid)); 
WebResponse response = request.GetResponse(); 

         Stream responseStream = response.GetResponseStream(); 

         // Open the stream using a StreamReader for easy access. 
         StreamReader reader = new StreamReader(responseStream); 

         string responseFromServer = reader.ReadToEnd(); 

上面的代码工作得很好,如果我没有通过不接受任何事情任何价值和调用Web服务。但是如果我必须通过两个价值观,那我该怎么做呢?

回答

0

REST Web服务只不过是HTTP请求更多。当你想使用HTTP发送一些东西时,你会发送与实际数据类型无关的字节。这就是人们在使用Web服务时经常使用JSON对象来描述参数和结果的原因。

This post描述了如何创建POST HTTP请求。在步骤7中,您在请求流中附加了一些字节。这是你应该追加你的论点的地方。

如果您看到完整的示例(向下滚动),您将看到使用以下代码片段创建了附加到流的字节数组。

// Create POST data and convert it to a byte array. 
string postData = "This is a test that posts this string to a Web server."; 
byte[] byteArray = Encoding.UTF8.GetBytes (postData); 

既然你只想传递简单的参数而不是对象,这就足够了。如果你需要传递对象作为参数,我会建议在postData 字符串中使用JSON。

希望我帮了忙!

相关问题