我正在尝试实现REST WCF服务以将文件从WPF应用程序发布到我的服务器,但是我收到405错误。在联系我的ISP之前,有人能告诉我我的代码是否正常?我知道这很可能与服务器不接受Post方法有关,但只是想确保我的代码没问题。这是: -
public static void UploadFile()
{
string serverPath = "http://www.mywebsites.com/test/";
string filePath = "C:\\Testing\\asd_asd_Feedback.xml";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverPath);
request.Accept = "text/xml";
request.Method = "PUT";
request.Credentials = new System.Net.NetworkCredential("testjo", "");
using (FileStream fileStream = File.OpenRead(filePath))
using (Stream requestStream = request.GetRequestStream())
{
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int byteCount = 0;
while ((byteCount = fileStream.Read(buffer, 0, bufferSize)) > 0)
{
requestStream.Write(buffer, 0, byteCount);
}
}
string result = String.Empty;
try
{
using (WebResponse response = request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
catch (Exception exc)
{
}
Console.WriteLine(result);
}
基本上它在request.GetResponse()
上失败了感谢您的帮助!