带有文件和参数的简单 Post 请求
Simple Post Request with file and parameters
我有这个代码,可以向本地快递 API 发出 post 请求。
public async Task ImportAwbIntegrat()
{
string fisier = File.ReadAllText(@"F:\Read\model.csv");
var values = new Dictionary<string, string>
{
{ "username", "clienttest" },
{ "client_id", "70321588" },
{"user_pass", "testing" },
{"fisier", fisier }
};
HttpClient client = new HttpClient();
try
{
var content = new FormUrlEncodedContent(values);
string methodurl = "https://www.selfawb.ro/import_awb_integrat.php";
var response = await client.PostAsync(methodurl, content);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response);
}
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
catch (Exception ex)
{
string message = ex.Message;
Console.WriteLine(message); ;
}
}
请求正常,
response value = {Method: POST, RequestUri: 'https://www.selfawb.ro/import_awb_integrat.php', Version: 1.1, Content: System.Net.Http.FormUrlEncodedContent, Headers:
{
Content-Type: application/x-www-form-urlencoded
Content-Length: 442
}}
但是在 responseString
里面我有这样的信息:
"The file was not uploaded. It asks you, try again."
我认为发生这种情况是因为服务器需要一个 .csv 文件,其值由 ; 分隔。但我发送的是文件中的值,而不是文件本身。
试试这个代码
public async Task ImportAwbIntegrat()
{
string fisier = File.ReadAllText(@"F:\Read\model.csv");
var values = new Dictionary<string, string>
{
{ "username", "clienttest" },
{ "client_id", "111111" },
{"user_pass", "testing" }
};
var image = File.ReadAllBytes(@"F:\Read\model.csv");
using (var client = new HttpClient())
{
using (var content =
new MultipartFormDataContent())
{
content.Add(new StreamContent(new MemoryStream(image)), "fisier", "model.csv");
foreach (KeyValuePair<string, string> param in values)
{
content.Add(new StringContent(param.Value), $"\"{param.Key}\"");
}
using (var response = await client.PostAsync("https://www.selfawb.ro/import_awb_integrat.php", content))
{
var input = await response.Content.ReadAsStringAsync();
var responseString = await response.Content.ReadAsStringAsync();
}
}
}
}
我有这个代码,可以向本地快递 API 发出 post 请求。
public async Task ImportAwbIntegrat()
{
string fisier = File.ReadAllText(@"F:\Read\model.csv");
var values = new Dictionary<string, string>
{
{ "username", "clienttest" },
{ "client_id", "70321588" },
{"user_pass", "testing" },
{"fisier", fisier }
};
HttpClient client = new HttpClient();
try
{
var content = new FormUrlEncodedContent(values);
string methodurl = "https://www.selfawb.ro/import_awb_integrat.php";
var response = await client.PostAsync(methodurl, content);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response);
}
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
catch (Exception ex)
{
string message = ex.Message;
Console.WriteLine(message); ;
}
}
请求正常,
response value = {Method: POST, RequestUri: 'https://www.selfawb.ro/import_awb_integrat.php', Version: 1.1, Content: System.Net.Http.FormUrlEncodedContent, Headers:
{
Content-Type: application/x-www-form-urlencoded
Content-Length: 442
}}
但是在 responseString
里面我有这样的信息:
"The file was not uploaded. It asks you, try again."
我认为发生这种情况是因为服务器需要一个 .csv 文件,其值由 ; 分隔。但我发送的是文件中的值,而不是文件本身。
试试这个代码
public async Task ImportAwbIntegrat()
{
string fisier = File.ReadAllText(@"F:\Read\model.csv");
var values = new Dictionary<string, string>
{
{ "username", "clienttest" },
{ "client_id", "111111" },
{"user_pass", "testing" }
};
var image = File.ReadAllBytes(@"F:\Read\model.csv");
using (var client = new HttpClient())
{
using (var content =
new MultipartFormDataContent())
{
content.Add(new StreamContent(new MemoryStream(image)), "fisier", "model.csv");
foreach (KeyValuePair<string, string> param in values)
{
content.Add(new StringContent(param.Value), $"\"{param.Key}\"");
}
using (var response = await client.PostAsync("https://www.selfawb.ro/import_awb_integrat.php", content))
{
var input = await response.Content.ReadAsStringAsync();
var responseString = await response.Content.ReadAsStringAsync();
}
}
}
}