使用Httpwebrequest上传文件 [英] Uploading file using Httpwebrequest
问题描述
我想将文件上传到服务器。我写了这个函数将文件上传到本地主机服务器(我使用的是wamp服务器):
private void button1_Click_1(object sender, EventArgs e)
{
FileStream fstream = new FileStream(@C:\ Users \Albert\Documents\10050409_3276.doc,FileMode.OpenOrCreate);
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(http:// localhost / upload_file);
request.Method =PUT;
request.ContentLength = fstream.Length;
request.AllowWriteStreamBuffering = true;
Stream request_stream = request.GetRequestStream();
byte [] indata = new byte [1024];
int bytes_read = fstream.Read(indata,0,indata.Length);
while(bytes_read> 0)
{
request_stream.Write(indata,0,indata.Length);
bytes_read = fstream.Read(indata,0,indata.Length);
}
fstream.Close();
request_stream.Close();
request.GetResponse();
MessageBox.Show(ok);
$ / code>
所以当我点击按钮时,异常apper说:
其他信息:远程服务器返回一个错误:(405)方法不允许。
我尝试使用POST而不是PUT,因此程序可以正常工作,并且消息框显示为ok,但是当我打开localhost-> upload_file(文件夹)Ididn'找到任何文件。
我用wamp server =>测试了我的程序。>发生了问题。
我测试过我的程序与真正的服务器,并放入网络凭据,并试图上传到文件夹(777)权限=>出现问题。
那么问题的确切位置在哪里?感谢:)
尝试使用webClient
WebClient client = new WebClient();
byte [] bret = client.UploadFile(path,POST,FilePath);
//路径== URL
// FilePath ==上传文件路径
或
WebClient webClient = new WebClient();
string webAddress = null;
尝试
{
webAddress = @http:// localhost / upload_file /;
webClient.Credentials = CredentialCache.DefaultCredentials;
WebRequest serverRequest = WebRequest.Create(webAddress);
serverRequest.Credentials = CredentialCache.DefaultCredentials;
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(路径,POST,FilePath);
webClient.Dispose();
webClient = null;
}
catch(异常错误)
{
MessageBox.Show(error.Message);
(代码或部分我没有试过)
I want to upload file to a server. I wrote this function to upload the file to localhost server (I am using wamp server):
private void button1_Click_1(object sender, EventArgs e)
{
FileStream fstream = new FileStream(@"C:\Users\Albert\Documents\10050409_3276.doc", FileMode.OpenOrCreate);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/upload_file");
request.Method = "PUT";
request.ContentLength = fstream.Length;
request.AllowWriteStreamBuffering = true;
Stream request_stream = request.GetRequestStream();
byte[] indata = new byte[1024];
int bytes_read = fstream.Read(indata, 0, indata.Length);
while (bytes_read > 0)
{
request_stream.Write(indata, 0, indata.Length);
bytes_read = fstream.Read(indata, 0, indata.Length);
}
fstream.Close();
request_stream.Close();
request.GetResponse();
MessageBox.Show("ok");
}
So when i click on the button the exception apper said that:
Additional information: The remote server returned an error: (405) Method Not Allowed.
I tried to use "POST" instead of "PUT" so the program works and the message box appears to say 'ok', but when i open the localhost->upload_file(folder) Ididn't find any files.
I tested my program with wamp server => the problem occured.
I tested my program with real server and put in the network credentials and tried to upload to folder that has (777) permission => the problem occured.
So where is the problem exactly?
Thanks :)
解决方案 try with webClient
WebClient client = new WebClient();
byte[] bret = client.UploadFile(path, "POST", FilePath);
//path==URL
//FilePath==Your uploading file path
or
WebClient webClient = new WebClient();
string webAddress = null;
try
{
webAddress = @"http://localhost/upload_file/";
webClient.Credentials = CredentialCache.DefaultCredentials;
WebRequest serverRequest = WebRequest.Create(webAddress);
serverRequest.Credentials = CredentialCache.DefaultCredentials;
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(path, "POST", FilePath);
webClient.Dispose();
webClient = null;
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
(code in or part i didn't tried )
这篇关于使用Httpwebrequest上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!