Restsharp 到 Post 一个 xml 文件到一个 api
Restsharp to Post An xml file to an api
我有一个 WindowsForm ADO.NET 应用程序,它将使用 post 方法调用 Flask api 并上传 xml 文件
这是我如何使用 python 调用 api :
import requests
API_URL = 'http://localhost:5000'
with open('result.xml') as fp:
content = fp.read()
response = requests.post(
'{}/files/result.xml'.format(API_URL), data=content
)
我一直在研究如何使用方法 post 调用 api 并以 c# windows 形式上传文件
这是我最终得到的:
上传功能:
private async Task<IRestResponse> UploadAsync(string fileName, string server)
{
var client = new RestClient(server);
var request = new RestRequest("/files", Method.POST);
XmlDocument doc = new XmlDocument();
// init XMLDocument and load customer in it
doc = new XmlDocument();
doc.Load(@""+fileName+"");
// Update (PUT) customer
request = new RestRequest("/files", Method.POST);
request.Parameters.Clear();
request.AddParameter("text/xml;charset=utf-8", doc.InnerXml, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response;
}
函数调用:
private async void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(label1.Text))
{
string url = "http://localhost:5000";
IRestResponse restResponse = await UploadAsync(label1.Text,url);
if (restResponse.StatusCode == System.Net.HttpStatusCode.OK)
MessageBox.Show("You have successfully uploaded the file", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
点击按钮不执行任何操作
没有错误,也没有发送文件
知道如何解决这个问题吗?
为了解决这个问题,我使用了这段代码:
XmlDocument doc = new XmlDocument();
doc = new XmlDocument();
doc.Load(@""+textBox1.Text+"");
var client = new RestClient("http://appapi9.azurewebsites.net/files/filethatillcheck.xml");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/xml");
request.AddParameter("application/xml",""+doc.OuterXml+"", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
MessageBox.Show("done");
我必须加载 xml 文件并发送它的内容
我有一个 WindowsForm ADO.NET 应用程序,它将使用 post 方法调用 Flask api 并上传 xml 文件
这是我如何使用 python 调用 api :
import requests
API_URL = 'http://localhost:5000'
with open('result.xml') as fp:
content = fp.read()
response = requests.post(
'{}/files/result.xml'.format(API_URL), data=content
)
我一直在研究如何使用方法 post 调用 api 并以 c# windows 形式上传文件
这是我最终得到的:
上传功能:
private async Task<IRestResponse> UploadAsync(string fileName, string server)
{
var client = new RestClient(server);
var request = new RestRequest("/files", Method.POST);
XmlDocument doc = new XmlDocument();
// init XMLDocument and load customer in it
doc = new XmlDocument();
doc.Load(@""+fileName+"");
// Update (PUT) customer
request = new RestRequest("/files", Method.POST);
request.Parameters.Clear();
request.AddParameter("text/xml;charset=utf-8", doc.InnerXml, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response;
}
函数调用:
private async void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(label1.Text))
{
string url = "http://localhost:5000";
IRestResponse restResponse = await UploadAsync(label1.Text,url);
if (restResponse.StatusCode == System.Net.HttpStatusCode.OK)
MessageBox.Show("You have successfully uploaded the file", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
点击按钮不执行任何操作 没有错误,也没有发送文件
知道如何解决这个问题吗?
为了解决这个问题,我使用了这段代码:
XmlDocument doc = new XmlDocument();
doc = new XmlDocument();
doc.Load(@""+textBox1.Text+"");
var client = new RestClient("http://appapi9.azurewebsites.net/files/filethatillcheck.xml");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/xml");
request.AddParameter("application/xml",""+doc.OuterXml+"", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
MessageBox.Show("done");
我必须加载 xml 文件并发送它的内容