I have a rest ful service (POST) which accepts Json object as an input (Request body in Fiddler). Now I wanted to consume the service from Console Applciation with dynamic values (either read from text file or hardcoded values). I will log the actions like, for this test data XXXXX, the service returns values.
我有一个rest ful服务(POST)接受Json对象作为输入(Fiddler中的Request body)。现在我想使用动态值(从文本文件或硬编码值读取)从Console Applciation使用服务。我将记录这些操作,对于此测试数据XXXXX,服务返回值。
Can any one help me how to automate this process. I wold like to comsume this service from Console application.
任何人都可以帮助我如何自动化这个过程。我希望从Console应用程序中提供此服务。
Pls note Output also JSON string.
请注意输出也是JSON字符串。
Any suggestion will be really helpful for me.
任何建议都对我有用。
2 个解决方案
#1
0
To make the POST request, something like:
要发出POST请求,例如:
var req = WebRequest.Create(url);
var enc = new UTF8Encoding(false);
var data = enc.GetBytes(serializedJson);
req.Method = "POST";
req.ContentType = "application/json";
req.ContentLength = data.Length;
using (var sr = req.GetRequestStream())
{
sr.Write(data, 0, data.Length);
}
var res = req.GetResponse();
var res= new StreamReader(res.GetResponseStream()).ReadToEnd();
You can easily create the serializedJson
from an object like:
您可以从以下对象轻松创建serializedJson:
var serializedJson = Newtonsoft.Json.JsonConvert.SerializeObject(myDataObject);
#2
0
Install the following web api package from nuget in your console project
在您的控制台项目中从nuget安装以下web api软件包
Add a reference to System.Net.Http and System.Runtime.Serialization
添加对System.Net.Http和System.Runtime.Serialization的引用
Create a contract class of the data you want to send and receive (the same as in your webservice)
创建要发送和接收的数据的合同类(与Web服务中的相同)
[DataContract]
public class YourObject{
[DataMember]
public int Id {get; set;}
}
In your console app call the webservice like this:
在您的控制台应用程序中调用Web服务,如下所示:
var client = new HttpClient();
var response = client.PostAsJsonAsync<YourObject>("http://yourserviceurl:port/controller", objectToPost).Result;
if(response.IsSuccessStatusCode){
Console.WriteLine(response);
}else{
Console.WriteLine(response.StatusCode);
}
More info about webapi here and here
有关webapi的更多信息,请点击此处
#1
0
To make the POST request, something like:
要发出POST请求,例如:
var req = WebRequest.Create(url);
var enc = new UTF8Encoding(false);
var data = enc.GetBytes(serializedJson);
req.Method = "POST";
req.ContentType = "application/json";
req.ContentLength = data.Length;
using (var sr = req.GetRequestStream())
{
sr.Write(data, 0, data.Length);
}
var res = req.GetResponse();
var res= new StreamReader(res.GetResponseStream()).ReadToEnd();
You can easily create the serializedJson
from an object like:
您可以从以下对象轻松创建serializedJson:
var serializedJson = Newtonsoft.Json.JsonConvert.SerializeObject(myDataObject);
#2
0
Install the following web api package from nuget in your console project
在您的控制台项目中从nuget安装以下web api软件包
Add a reference to System.Net.Http and System.Runtime.Serialization
添加对System.Net.Http和System.Runtime.Serialization的引用
Create a contract class of the data you want to send and receive (the same as in your webservice)
创建要发送和接收的数据的合同类(与Web服务中的相同)
[DataContract]
public class YourObject{
[DataMember]
public int Id {get; set;}
}
In your console app call the webservice like this:
在您的控制台应用程序中调用Web服务,如下所示:
var client = new HttpClient();
var response = client.PostAsJsonAsync<YourObject>("http://yourserviceurl:port/controller", objectToPost).Result;
if(response.IsSuccessStatusCode){
Console.WriteLine(response);
}else{
Console.WriteLine(response.StatusCode);
}
More info about webapi here and here
有关webapi的更多信息,请点击此处