好的,這是交易。正如問題所述,我試圖將文件發佈到網絡服務器,並且遇到了一些問題。使用.NET將文件發佈到服務器HttpWebRequest或WebClient
我試過把這個文件發佈到使用Curl.exe的同一個web服務器上,並且沒有問題。我已經發布了使用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
我試圖使用.NET類像WebClient或HttpWebRequest做同樣的事情。這是我試過的代碼的一個例子。使用WebClient,我得到一個505 HTTP版本不支持的錯誤,並且使用HttpWebRequest,我得到了501未實現。
當使用Web客戶端嘗試它:
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();
}
使用調試http代理看出差別正是在導線上的兩個電話之間。試試Charles(http://www.charlesproxy.com/)或Fiddler(http://www.fiddler2.com/fiddler2/)。 – 2010-03-02 04:27:36