我有Windows应用程序,该应用程序使用Web客户端调用WebAPi。
问题:
当我们托管Web服务并从Visual Studio运行的Windows应用程序中调用它时,它可以正常工作。但是,当我们将应用程序发送到另一台笔记本电脑时,WEbAPI会将参数接收为null。
Windows应用程序功能:
public static DataSet Add(DataTable data, DataTable settings, string username, string pwd)
{
DataSet Result = new DataSet();
try
{
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + pwd)));
DataSet set = new DataSet("parameter");
set.Tables.Add(data.Copy());
set.Tables.Add(settings.Copy());
string param_value = JsonConvert.SerializeObject(set);
string result = client.UploadString("http://localhost:3535/Add", param_value);
JsonResult value = JsonConvert.DeserializeObject<JsonResult>(result);
if (value.Data != null)
Result = JsonConvert.DeserializeObject<DataSet>(value.Data.ToString());
}
}
catch (Exception err)
{
}
return Result;
}
WEB API函数:
[BasicAuthentication]
[System.Web.Http.HttpPost]
public JsonResult Add([FromBody] JObject parameter)
{
JsonResult Result = new JsonResult();
try
{
DataTable Data = new DataTable();
DataSet set = new DataSet("response");
DataSet DataSetResult = parameter.ToObject<DataSet>();
DataTable values = Add(DataSetResult.Tables[0], DataSetResult.Tables[1], out Data);
set.Tables.Add(Data.Copy());
set.Tables.Add(values.Copy());
Result.Data = set;
Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
catch (Exception err)
{
}
return Result;
}