我正在使用此代码将参数发送到网页,并从中获取正确的响应.
System.Net.WebClient oWeb = new System.Net.WebClient(); oWeb.Proxy = System.Net.WebRequest.DefaultWebProxy; oWeb.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; oWeb.Headers.Add("Content-Type","application/x-www-form-urlencoded"); byte[] bytArguments = System.Text.Encoding.ASCII.GetBytes("value1=123&value2=xyz"); byte[] bytRetData = oWeb.UploadData("http://website.com/file.PHP","POST",bytArguments); response = System.Text.Encoding.ASCII.GetString(bytRetData);
但是现在我想发送一个像.doc这样的文件到上面的参数(value1,value2),但是我不知道该怎么做.
解决方法
使用
WebClient.QueryString传递与请求关联的名称/值对.
NameValueCollection parameters = new NameValueCollection(); parameters.Add("value1","123"); parameters.Add("value2","xyz"); oWeb.QueryString = parameters; var responseBytes = oWeb.UploadFile("http://website.com/file.PHP","path to file"); string response = Encoding.ASCII.GetString(responseBytes);