请问使用这个类能在 请求头里面 加进Cookie吗?
一下是MSDN里的代码 string uriString;
Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
uriString = Console.ReadLine(); // Create a new WebClient instance.
WebClient myWebClient = new WebClient();
Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
string postData = Console.ReadLine();
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); // Display the headers in the request
Console.Write("Resulting Request Headers: ");
Console.WriteLine(myWebClient.Headers.ToString());
// Apply ASCII Encoding to obtain the string as a byte array. byte[] byteArray = Encoding.ASCII.GetBytes(postData);
Console.WriteLine("Uploading to {0} ...", uriString);
// Upload the input string using the HTTP 1.0 POST method.
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
// Decode and display the response.
Console.WriteLine("\nResponse received was {0}",
Encoding.ASCII.GetString(responseArray));
如果我要提交cooike 怎么办? 因为是要把图片下载下来,所以必须使用webclient这个类
UploadData这个函数一定要发送byteArray,那我只发送HTTP请求不需要POST什么东西该怎么办呢?
一下是MSDN里的代码 string uriString;
Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
uriString = Console.ReadLine(); // Create a new WebClient instance.
WebClient myWebClient = new WebClient();
Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
string postData = Console.ReadLine();
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); // Display the headers in the request
Console.Write("Resulting Request Headers: ");
Console.WriteLine(myWebClient.Headers.ToString());
// Apply ASCII Encoding to obtain the string as a byte array. byte[] byteArray = Encoding.ASCII.GetBytes(postData);
Console.WriteLine("Uploading to {0} ...", uriString);
// Upload the input string using the HTTP 1.0 POST method.
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
// Decode and display the response.
Console.WriteLine("\nResponse received was {0}",
Encoding.ASCII.GetString(responseArray));
如果我要提交cooike 怎么办? 因为是要把图片下载下来,所以必须使用webclient这个类
UploadData这个函数一定要发送byteArray,那我只发送HTTP请求不需要POST什么东西该怎么办呢?
如果你有这个需求的话,就不应该用WebClient的,你应该用 HttpRequest
HttpRequest 可以在请求时加Cookies,具体有什么区别,你还是baidu吧
command.Headers.Add("Cookie", cookie);
你朋友不是一样使用这个类提交吗?
HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
request.Method = "GET";
request.KeepAlive = true;
//接收传来的值
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
Bitmap OldBitmap = Image.FromStream(responseStream) as Bitmap;
OldBitmap.Save("c:\\button.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);问题自己解决了,大家有兴趣运行下这段代码!