好的,这就是交易。正如问题所述,我正在尝试将文件发布到网络服务器并且遇到了一些问题。
我尝试使用 Curl.exe 将相同的文件发布到同一个网络服务器并且没有任何问题。我已经发布了我在 curl 中使用的标志,以防它们指出我在使用 .NET 类时遇到问题的任何潜在原因。
curl.exe --user "myUser:myPass" --header "Content-Type: application/gzip"
--data-binary "@filename.txt.gz" --cookie "data=service; data-ver=2; date=20100212;
time=0900; location=1234" --output "out.txt" --dump-header "header.txt"
http://mysite/receive
我正在尝试使用像 WebClient 或 HttpWebRequest 这样的 .NET 类来做同样的事情。这是我尝试过的代码示例。使用 WebClient 我得到一个 505 HTTP Version Not Supported 错误,使用 HttpWebRequest 我得到一个 501 Not Implemented。
使用 WebClient 尝试时:
public void sendFileClient(string path){
string url = "http://mysite/receive";
WebClient wc = new WebClient();
string USERNAME = "myUser";
string PSSWD = "myPass";
NetworkCredential creds = new NetworkCredential(USERNAME, PSSWD);
wc.Credentials = creds;
wc.Headers.Set(HttpRequestHeader.ContentType, "application/gzip");
wc.Headers.Set("Cookie", "location=1234; date=20100226; time=1630; data=service; data-ver=2");
wc.UploadFile(url, "POST", path);
}
在使用 HttpRequest 时:
public Stream sendFile(string path)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://myserver/receive");
string USERNAME = "myUser";
string PSSWD = "myPass";
NetworkCredential creds = new NetworkCredential(USERNAME, PSSWD);
request.Credentials = creds;
request.Method = "POST";
request.ContentType = "application/gzip";
request.Headers.Set("Cookie", "location=1234; date=20100226; time=1630; data=service; data-ver=2");
FileInfo fInfo = new FileInfo(path);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
byte[] data = br.ReadBytes((int)numBytes);
br.Close();
fStream.Close();
fStream.Dispose();
Stream wrStream = request.GetRequestStream();
BinaryWriter bw = new BinaryWriter(wrStream);
bw.Write(data);
bw.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response.GetResponseStream();
}