Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpWebRequest.GetResponse()无法使用c中的HttpPropostedFile#_C#_Asp.net_Kentico - Fatal编程技术网

C# HttpWebRequest.GetResponse()无法使用c中的HttpPropostedFile#

C# HttpWebRequest.GetResponse()无法使用c中的HttpPropostedFile#,c#,asp.net,kentico,C#,Asp.net,Kentico,我正在肯蒂科的一个项目中工作,我必须将MetaScan集成到肯蒂科。我正试图通过方法callMetaScan(CMSFileUpload.PostedFile)发送一个HttpPosted文件,我有以下callMetaScan方法 private void callMetaScan(System.Web.HttpPostedFile httpPostedFile){ string strDate = string.Format("{0} GMT", DateTime.Now.ToUnive

我正在肯蒂科的一个项目中工作,我必须将MetaScan集成到肯蒂科。我正试图通过方法
callMetaScan(CMSFileUpload.PostedFile)
发送一个
HttpPosted
文件,我有以下callMetaScan方法

private void callMetaScan(System.Web.HttpPostedFile httpPostedFile){ 


string strDate = string.Format("{0} GMT", DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss"));


System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(myURI);


myReq.Date = Convert.ToDateTime(strDate);


myReq.Method = "POST";


byte[] myBytes = System.Text.Encoding.UTF8.GetBytes(StreamToString(httpPostedFile1.InputStream));‌


//byte[] myBytes = new byte[httpPostedFile.InputStream.Length];
//Read the file into the byte array.


httpPostedFile.InputStream.Read(myBytes, 0, myBytes.Length);


myReq.ContentLength = myBytes.Length;
myReq.ContentType = httpPostedFile.ContentType;


System.IO.Stream PostData = (System.IO.Stream)myReq.GetRequestStream();
PostData.Write(myBytes, 0, myBytes.Length);
PostData.Close();


System.Net.HttpWebResponse Response2 = (System.Net.HttpWebResponse)myReq.GetResponse();



System.IO.StreamReader SR = default(System.IO.StreamReader);
SR = new System.IO.StreamReader(Response2.GetResponseStream());
string rslt = SR.ReadToEnd();
SR.Close();
System.Xml.XmlDocument xmlRslt = new System.Xml.XmlDocument();
xmlRslt.LoadXml(rslt);

//if no final_scan_result, then save a copy of the xml and email us.
if (xmlRslt.SelectSingleNode(pathstring) != null)
{
    int finalScanResultCode = Int32.Parse(xmlRslt.SelectSingleNode(pathstring).InnerText);

}
else
{
    //Send Email about the XML
     Response.Write(“Unable to scan file.”);            
}
}
但是我的代码在请求response System.Net.HttpWebResponse Response2=(System.Net.HttpWebResponse)myReq.GetResponse()后不起作用; 就连我也不例外

请建议我如何解决这个问题。我真的很感激任何帮助


谢谢

您有一个名为“StreamToString”的方法,它可能有问题。这看起来像是将输入文件转换为文本,然后再转换为二进制文件。然后你甚至把字节读回发布文件的输入流?不确定那是否应该在那里。另外,我认为这不是问题所在,但您应该合并一些using语句,以确保所有流在适当的时间关闭。还有一件事,您是否需要添加某种键/头,以便MetaScan API知道您是谁?非常感谢John的回复和建议。现在我的代码正在运行。你抓住了正确的要点。因为我正在对文件进行编码,所以在上传图像后,我没有得到实际的图像文件。这是我的
StreamToString
方法(我从stackoverflow的另一个问题帖子中获取)
公共静态字符串StreamToString(System.IO.streamdata){System.IO.StreamReader reader=new System.IO.StreamReader(data);字符串体=reader.ReadToEnd();reader.Close();reader.Dispose();return body;}
现在我可以上传图像文件,但问题是它已经编码,所以我无法看到实际图像。请在此帮助我,如果我没有编码的文件,那么它给我的位置已经改变错误。我在当前编码之前使用过这段代码一个
byte[]myBytes=newbyte[myfile.PostedFile.InputStream.Length]并得到了那个错误。