我们有这个软件有一个webservices组件,想通过使用webservices组件将数据导入到系统中。
文档给出了使用HTTP动词(如GET,POST,PUT,DELETE)与系统交互的示例。
我构建了具有method =“post”或method =“get”的HTML表单,并将请求发送给action属性中指定的任何内容(action =“someResource”)。但我不知道该怎么做。
发布于 2018-04-17 14:43:19
这是一个使用HttpWebRequest的C#示例:
using System;
using System.IO;
using System.Net;
class Test
{
static void Main()
{
string xml = "<xml>...</xml>";
byte[] arr = System.Text.Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/");
request.Method = "PUT";
request.ContentType = "text/xml";
request.ContentLength = arr.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(arr, 0, arr.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string returnString = response.StatusCode.ToString();
Console.WriteLine(returnString);
}
}
现在在System.Net.Http中有一个HttpClient类(可作为NuGet包使用),这使得它更容易一些:
using System;
using System.Net.Http;
class Program
{
static void Main()
{
var client = new HttpClient();
var content = new StringContent("<xml>...</xml>");
var response = client.PutAsync("http://localhost/", content).Result;
Console.WriteLine(response.StatusCode);
}
}
发布于 2018-04-17 15:30:33
PUT和DELETE可能需要使用AJAX并创建XMLHttpRequests,因为FORM标记仅支持GET和POST动词,而链接仅会发出GET请求。
使用jQuery:
$.ajax( {
url: '/controller/action',
type: 'PUT',
data: function() { ...package some data as XML },
dataType: 'xml',
... more options...
);
关于jQuery ajax选项页面的说明警告说,某些浏览器不支持请求类型的PUT和DELETE。我从来没有使用PUT,但在IE和FF中使用了DELETE。尚未在Safari或Opera中进行测试。
https://stackoverflow.com/questions/-100008141
复制相似问题