8

好的,这就是交易。正如问题所述,我正在尝试将文件发布到网络服务器并且遇到了一些问题。

我尝试使用 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();  
}
4

2 回答 2

10

首先,使用 fiddler 之类的东西并检查请求和响应,看看 curl 和 System.Net.WebClient 之间有什么不同。

此外,您可以尝试(尽管使用调试代理进行检查应该可以让您查明差异):

使用凭证缓存设置您的凭证以进行基本身份验证:

var cc= new CredentialCache();
cc.Add(new Uri(url),
                  "Basic", 
                  new NetworkCredential("USERNAME", "PASSWORD"));
wc.Credentials = cc;

设置用户代理标头:

string _UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
wc.Headers.Add(HttpRequestHeader.UserAgent, _UserAgent);

更改 WebRequest 上的协议版本:

reqeust.KeepAlive = false;
request.ProtocolVersion=HttpVersion.Version10;
于 2010-03-02T04:31:16.917 回答
1

There might be another 2 reasons when a 501 accord.

----------1---------

when the postdate contain some Chinese Chracter or some other character. e.g.

postDate = "type=user&username=计算机学院&password=123&Submit=+登录+"

in order post the right message,you may also add following 2 line;

Request.SendChunked = true;
Request.TransferEncoding = "GB2312";

this also lead to a 501.

in that occasion,you can delete the 2 line,and modify postDate like so.

postDate = "type=user&username=%BC%C6%CB%E3%BB%FA%D1%A7%D4%BA&password=123&Submit=+%C8%B7%C8%CF+"

maybe this is a solution to modify the postDate,however i havn't test yet.

string str = Encoding.GetEncoding("gb2312").GetString(tmpBytes);

----------2---------

if Response.StatusCode == HttpStatusCode.Redirect Redirect is equals to 302. following line is a must:</p>

Request.AllowAutoRedirect = false;
于 2011-04-23T02:21:45.070 回答