C# 将HttpWebRequest对象传递给Web API方法是否过于明确?
我知道,至少我认为是这样,当调用Web API方法时,客户端会向它传递一个HttpWebRequest。注意,当客户端发送类似这样的内容以调用Web API方法时:C# 将HttpWebRequest对象传递给Web API方法是否过于明确?,c#,asp.net-web-api,httpwebrequest,getresponsestream,frombodyattribute,C#,Asp.net Web Api,Httpwebrequest,Getresponsestream,Frombodyattribute,我知道,至少我认为是这样,当调用Web API方法时,客户端会向它传递一个HttpWebRequest。注意,当客户端发送类似这样的内容以调用Web API方法时: 192.168.125.50:28642/api/DeliveryItems/PostArgsAndXMLFileAsStr?serialNum=42&siteNum=99 [HttpPost] [Route("api/deliveryitems/InsertIntoPT109Data")] public void In
192.168.125.50:28642/api/DeliveryItems/PostArgsAndXMLFileAsStr?serialNum=42&siteNum=99
[HttpPost]
[Route("api/deliveryitems/InsertIntoPT109Data")]
public void InsertIntoPT109Data(HttpWebRequest httpwebreq)
{
HHSService.InsertIntoPPTData(httpwebreq);
}
…将包含serialNum 42和siteNum 99值的HttpWebRequest对象传递给该方法
我还认为,无论如何,到目前为止,如果大量数据需要作为URI的一部分发送过多/过多,如示例中的serialNum和siteNum,则可以将这些数据嵌入客户端上的HttpWebRequest对象中,如下所示:
string data = "bla blee bloo blah foo bar doo dah";
WebRequest request = WebRequest.Create(uri);
. . .
byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream())
{
oS.Write(arrData, 0, arrData.Length);
}
WebResponse response = request.GetResponse();
public void PostArgsAndXMLFileAsStr([FromBody] string stringifiedXML, string serialNum, string siteNum)
{
XDocument doc = XDocument.Parse(stringifiedXML);
[Test]
public void TestNRBQDeliveryItemInterfacePostPPTData()
{
var DeliveryItem = IOC.container.Resolve<INRBQDeliveryItem>();
string data = @"<Command>
<DSD>
<line_id>1</line_id>
. . .
<discount>0.25</discount>
<fileDate>12/19/2013 1:33:27 PM</fileDate>
</DSD>
</Command>";
string uri = "http://localhost:21609/api/deliveryitems/InsertIntoPPTData";
WebRequest request = WebRequest.Create(uri);
request.Method = Enum.ToObject(typeof(HttpMethods), HttpMethods.POST).ToString();
request.ContentType = "application/json";
((HttpWebRequest)request).Accept = request.ContentType;
((HttpWebRequest)request).KeepAlive = false;
((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;
Encoding encoding = Encoding.UTF8; // needed?
byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream())
{
oS.Write(arrData, 0, arrData.Length);
}
// Send the request to the server by calling GetResponse. (http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx)
WebResponse response = request.GetResponse();
Assert.IsNotNull(response);
}
…然后在另一个服务器端使用[FromBody]属性进行解码,如下所示:
string data = "bla blee bloo blah foo bar doo dah";
WebRequest request = WebRequest.Create(uri);
. . .
byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream())
{
oS.Write(arrData, 0, arrData.Length);
}
WebResponse response = request.GetResponse();
public void PostArgsAndXMLFileAsStr([FromBody] string stringifiedXML, string serialNum, string siteNum)
{
XDocument doc = XDocument.Parse(stringifiedXML);
[Test]
public void TestNRBQDeliveryItemInterfacePostPPTData()
{
var DeliveryItem = IOC.container.Resolve<INRBQDeliveryItem>();
string data = @"<Command>
<DSD>
<line_id>1</line_id>
. . .
<discount>0.25</discount>
<fileDate>12/19/2013 1:33:27 PM</fileDate>
</DSD>
</Command>";
string uri = "http://localhost:21609/api/deliveryitems/InsertIntoPPTData";
WebRequest request = WebRequest.Create(uri);
request.Method = Enum.ToObject(typeof(HttpMethods), HttpMethods.POST).ToString();
request.ContentType = "application/json";
((HttpWebRequest)request).Accept = request.ContentType;
((HttpWebRequest)request).KeepAlive = false;
((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;
Encoding encoding = Encoding.UTF8; // needed?
byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream())
{
oS.Write(arrData, 0, arrData.Length);
}
// Send the request to the server by calling GetResponse. (http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx)
WebResponse response = request.GetResponse();
Assert.IsNotNull(response);
}
我的假设正确吗?对于这样的测试代码:
string data = "bla blee bloo blah foo bar doo dah";
WebRequest request = WebRequest.Create(uri);
. . .
byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream())
{
oS.Write(arrData, 0, arrData.Length);
}
WebResponse response = request.GetResponse();
public void PostArgsAndXMLFileAsStr([FromBody] string stringifiedXML, string serialNum, string siteNum)
{
XDocument doc = XDocument.Parse(stringifiedXML);
[Test]
public void TestNRBQDeliveryItemInterfacePostPPTData()
{
var DeliveryItem = IOC.container.Resolve<INRBQDeliveryItem>();
string data = @"<Command>
<DSD>
<line_id>1</line_id>
. . .
<discount>0.25</discount>
<fileDate>12/19/2013 1:33:27 PM</fileDate>
</DSD>
</Command>";
string uri = "http://localhost:21609/api/deliveryitems/InsertIntoPPTData";
WebRequest request = WebRequest.Create(uri);
request.Method = Enum.ToObject(typeof(HttpMethods), HttpMethods.POST).ToString();
request.ContentType = "application/json";
((HttpWebRequest)request).Accept = request.ContentType;
((HttpWebRequest)request).KeepAlive = false;
((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;
Encoding encoding = Encoding.UTF8; // needed?
byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream())
{
oS.Write(arrData, 0, arrData.Length);
}
// Send the request to the server by calling GetResponse. (http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx)
WebResponse response = request.GetResponse();
Assert.IsNotNull(response);
}
…改为:
[HttpPost]
[Route("api/deliveryitems/InsertIntoPT109Data")]
public void InsertIntoPT109Data([FromBody] stringifiedXML)
{
HHSService.InsertIntoPT109Data(stringifiedXML);
}
?
为了更简单,我认为使用HttpWebRequest对象作为Web API方法的参数太显式了;我认为/希望发送的HttpWebRequest是给定的,在本例中我需要的是[FromBody]stringifiedXML参数,然后我可以解压[FromBody]数据,就像我上面给出的示例一样,例如,XDocument doc=XDocument.ParsestringifiedXML
我的现有代码显式地将HttpWebRequest对象作为方法的arg传递,或者我假设HttpWebRequest对象隐式地存在,并处理嵌入其中的[FromBody]数据,这两种情况我都是正确的吗
如果我在第一个/当前的情况下必须传递HttpWebRequest对象,那么如何获取HttpWebRequest对象中的嵌入数据?我需要打电话给getResponseStream还是
使现代化
如果我能够放弃HttpWebRequest的显式使用,那么还有一个问题。这:
void InsertIntoPT109Data([FromBody] stringifiedXML);
…不作为接口方法编译,因为无法识别[FromBody]属性。用字符串替换它就足够了吗
更新2
我已将HttpWebRequest httpwebreqs替换为[FromBody]字符串stringifiedXML,例如:
public void InsertIntoPT109Data([FromBody] String stringifiedXML)
…但现在我发现,找不到类型或命名空间名称“FromBody”是否缺少using指令或程序集引用
无论是否使用System.Web.Http添加,我都会得到这个结果;或者不是
如果需要将DLL添加到项目引用中,它是哪个DLL?不会向服务器发送或从服务器接收HttpWebRequest/HttpWebResponse。它们用于向服务器发送数据和从服务器接收数据
你的假设是错误的。我认为@paulo morgado很好地涵盖了第一部分,我没有更多的补充 关于文章末尾的FromBody更新,有两件事需要注意: 您需要为stringifiedXML指定一个类型,即使您使用[FromBody],我假定为string,并且 您需要添加: 使用System.Web.Http; 在您可以使用[FromBody]之前,在文件的顶部。有时人们会将其与System.Web.Mvc混淆,后者是特定于Mvc项目而非Web API的。
我得到的类型或命名空间名称“Http”在命名空间“System.Web”中不存在。是否缺少程序集引用?在using子句中,我添加了using System.Web.Http@你在使用WebAPI2吗?此属性是在v2或5.0或Web API中添加的。@B.ClayShannon如果您没有此属性,您可以查看该属性的说明。@B.ClayShannon检查您的packages文件夹下,以查找Microsoft.AspNet.WebApi.Core.5.1.0或类似文件,并确保在其文件夹结构的lib内的某个位置有文件System.Web.Http.dll。另外,不要担心这50个项目,您只关心您试图添加FromBody属性的项目。@B.ClayShannon要么引用项目中的节,要么引用文件系统中的packages文件夹。此外,请查看项目中的文件packages.config,其中应该有一行与以下内容类似的内容:。