问题描述
I'm using this code to send parameters to a webpage and getting correct response from it.
使用此代码将参数发送到网页并获得正确的响应。 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);
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
),但我不知道如何做到这一点。
But now I want to send a file like (.doc
) to it + above parameters(value1
, value2
), but I don't know how to do that.
推荐答案
使用传递与请求关联的名称/值对。
Use WebClient.QueryString to pass name/value pairs associated with the request.
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);
这篇关于在发布请求中发送文件+参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!