使用.NET发布一个文件到服务器的HttpWebRequest或Web客户端客户端、服务器、文件、NET

2023-09-03 03:27:21 作者:余生愛浪

好了,所以这里的交易。因为这个问题的国家,我想发布一个文件到网络服务器时遇到的一些问题。

我已经试过张贴此相同的文件使用Curl.exe相同的网络服务器暂时还没有问题。我已为我所用,卷曲的标志只是柜面他们可能会指出为什么我无法用.NET类的潜在原因。

  curl.exe --userMYUSER:为mypass--header内容类型:应用程序/ GZIP
--data二进制@ filename.txt.gz--cookie数据=服务;数据版本= 2;日期= 20100212;
时间= 0900;位置= 1234 - 输出out.txt自卸头header.txt
HTTP:// mysite的/接收
 

我试图用一个.NET类像Web客户端或HttpWebRequest的做同样的事情。这里是code我已经尝试了样品。随着Web客户端,我得到一个505的HTTP版本不支持的错误,并与HttpWebRequest的,我收到了501未执行。

当一个Web客户端尝试它:

 公共无效sendFileClient(字符串路径){
    字符串的URL =HTTP:// mysite的/接收;
    Web客户端WC =新的Web客户端();
    字符串username =MYUSER;
    字符串PSSWD =为mypass;

    的NetworkCredential creds =新的NetworkCredential(用户名,PSSWD);
    wc.Credentials = creds;
    wc.Headers.Set(Htt的prequestHeader.ContentType,应用程序/ GZIP);
    wc.Headers.Set(曲奇,位置= 1234;日期= 20100226;时间= 1630;数据=服务;数据版本= 2);

    wc.UploadFile(URL,POST,路径);
}
 
怎样查看服务器是否安装 netfarneworke 4.5

虽然使用的Htt prequest:

 公共流SENDFILE(字符串路径)
{
    HttpWebRequest的要求=(HttpWebRequest的)HttpWebRequest.Create(HTTP:// MYSERVER /接收);
    字符串username =MYUSER;
    字符串PSSWD =为mypass;
    的NetworkCredential creds =新的NetworkCredential(用户名,PSSWD);
    request.Credentials = creds;
    request.Method =POST;
    request.ContentType =应用/ GZIP;
    request.Headers.Set(曲奇,位置= 1234;日期= 20100226;时间= 1630;数据=服务;数据版本= 2);
    FileInfo的fInfo =新的FileInfo(路径);
    长的numBytes = fInfo.Length;
    的FileStream fstream的=新的FileStream(路径,FileMode.Open,FileAccess.Read);
    BR BinaryReader在新= BinaryReader在(fstream的);
    byte []的数据= br.ReadBytes((INT)的numBytes);
    br.Close();
    fStream.Close();
    fStream.Dispose();
    流wrStream = request.GetRequestStream();
    的BinaryWriter体重=新的BinaryWriter(wrStream);
    bw.Write(数据);
    bw.Close();
    HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
    返回response.GetResponseStream();
}
 

解决方案

首先,使用这样的小提琴手和检查请求和响应,看看有什么卷曲和System.Net.WebClient之间是不同的。

此外,您还可以尝试(虽然与调试代理检查应该让你精确定位的差异):

使用凭证高速缓存设置您的凭据用于基本身份验证:

 变种CC =新CredentialCache();
cc.Add(新的URI(URL),
                  基本,
                  新的NetworkCredential(用户名,密码));
wc.Credentials = CC;
 

设置用户代理头:

 字符串_UserAgent =Mozilla的/ 4.0(兼容; MSIE 6.0; Windows NT的5.2; .NET CLR 1.0.3705;);
wc.Headers.Add(Htt的prequestHeader.UserAgent,_UserAgent);
 

更改协议版本上的WebRequest:

  reqeust.KeepAlive = FALSE;
request.ProtocolVersion = HttpVersion.Version10;
 

Okay so here is the deal. As the question states, I'm trying to POST a file to a webserver and am having a few issues.

I've tried posting this same file to the same webserver using Curl.exe and have had no issues. I've posted the flags I used with curl just incase they might point out any potential reasons why I'm having trouble with the .NET classes.

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

I'm trying to use a .NET class like WebClient or HttpWebRequest to do the same thing. Here is a sample of the code I've tried. With the WebClient I get a 505 HTTP Version Not Supported error and with the HttpWebRequest I get a 501 Not Implemented.

When trying it with a 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);
}

And while using a 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();  
}

解决方案

First, use something like fiddler and inspect the requests and responses to see what differs between curl and System.Net.WebClient.

Also, you can try (although inspecting with the debugging proxy should allow you to pinpoint the difference):

Use the credential cache to set your credentials for basic authentication:

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

Set a user agent header:

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

Change the protocol version on the WebRequest:

reqeust.KeepAlive = false;
request.ProtocolVersion=HttpVersion.Version10;

 
精彩推荐
图片推荐