gpt4 book ai didi

c# - 使用 wcf restful 服务通过 WebRequest 的原始主体发送和接收数据?

转载 作者:行者123 更新时间:2023-11-30 23:32:30 25 4
gpt4 key购买 nike

我已经实现了一个 WCF RESTful web 服务,它在 http POST 期间通过原始主体获取参数。当通过 "POSTMAN" chrome 扩展进行测试时,它会给出如下所示的预期结果图片:

REST service url and raw body parameters

现在我想在使用以下代码通过 ASP Web 应用程序使用相同服务的同时获得相同的结果

JavaScriptSerializer ser = new JavaScriptSerializer();
var request = (HttpWebRequest)WebRequest.Create("http://192.168.0.19/SSARESTservice/SSA/IMEIVerification");
request.Method = "POST";
request.ContentType = "application/json";
DeviceData data = new DeviceData() { IMEI = "352423061590616" };
string str = ser.Serialize(data);
byte[] databyte = System.Text.Encoding.UTF8.GetBytes(str);
request.ContentLength = databyte.Length;
var reqStrm = request.GetRequestStream();
reqStrm.Write(databyte, 0, databyte.Length);

var response = (HttpWebResponse)request.GetResponse();
var respStrm = response.GetResponseStream();
StreamReader reader = new StreamReader(respStrm);
string bodyprm = reader.ReadToEnd();

但是在执行上面的代码时出现如下错误

The remote server returned an error: (400) Bad Request.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

第 34 行:var response = (HttpWebResponse)request.GetResponse();

这是我的操作契约(Contract)

[WebInvoke(UriTemplate = "IMEIVerification", Method = "POST",  ResponseFormat= WebMessageFormat.Json, RequestFormat =       WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)
[OperationContract]
JsonResponseProps VerifyDevice(DeviceData data);

和实现

public JsonResponseProps VerifyDevice(DeviceData data)
{
JsonResponseProps jis = new JsonResponseProps();
using (con = new SqlConnection(connection))
{
con.Open();
cmd = new SqlCommand("select IMEI from dbo.tbl_master_Devicedata where IMEI='" + data.IMEI + "'", con);
var IMEI = cmd.ExecuteScalar();
if (IMEI != null)
{
jis.msg = "Verification Successful";
jis.success = "true";
}
else
{
jis.msg = "Verification Failed";
jis.success = "false";
}
con.Close();
}
return jis;
}

谢谢

最佳答案

问题是你使用 POSTMAN 发送:

{"data":{"IMEI":"352423061590616"}}

但是当你检查来自

的序列化数据时
JavaScriptSerializer ser = new JavaScriptSerializer();

DeviceData data = new DeviceData() { IMEI = "352423061590616" };

string str = ser.Serialize(data);

您会看到线格式是:

{"IMEI":"352423061590616"}

这并不能很好地映射到您的 data 参数上。

要解决此问题,请为 Root 容器添加一个额外的类。它有一个 DeviceData 类型的数据属性来保存您的实际负载。序列化的代码如下所示:

// container with a data property
// this one is being serialized
public class Root
{
public DeviceData data { get; set; }
}

public class DeviceData
{
public string IMEI { get; set; }
}

protected void CallIMEIVerfication()
{
JavaScriptSerializer ser = new JavaScriptSerializer();
DeviceData data = new DeviceData() { IMEI = "352423061590616" };
var root = new Root { data=data };
string str = ser.Serialize(root);
// rest of your code
}

关于c# - 使用 wcf restful 服务通过 WebRequest 的原始主体发送和接收数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34290322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com