请求地址:http://192.168.0.124/testwebapi/api/AddUser
请求方式:Post
验证方式:用户名密码等信息放在Http Header 提交.
Account=用户名
pwd =密码
rid =3
参数示例:{"id": "123","name": "张建"}
参数说明:
字段参数类型参数说明参数范围说明
id string 编号
name string 名称
$("#button").click(function() {
$.ajax({
type: "POST",
url: "http://192.168.0.124/testwebapi/api/AddUser",
beforeSend: function(request) {
request.setRequestHeader("ID", "123");
request.setRequestHeader("name", "张建");
},
success: function(result) {
alert(result);
}
});
});
这个是后台部分
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Account", "aaa");
client.DefaultRequestHeaders.Add("pwd", "bbb");
client.DefaultRequestHeaders.Add("rid ", "1");
HttpResponseMessage response = client.PostAsJsonAsync<Customer>(url, book).Result;
string json = response.Content.ReadAsStringAsync().Result;
***分隔符***
我一般习惯于使用HttpWebRequest,可以这样
public virtual HttpWebRequest CreateWebRequest(string uri,
NameValueCollection collHeader,
string RequestMethod, bool NwCred)
{
HttpWebRequest webrequest =
(HttpWebRequest) WebRequest.Create(uri);
webrequest.KeepAlive = false;
webrequest.Method = RequestMethod;
int iCount = collHeader.Count;
string key;
string keyvalue;
for (int i=0; i < iCount; i++)
{
key = collHeader.Keys[i];
keyvalue = collHeader[i];
webrequest.Headers.Add(key, keyvalue);
}
webrequest.ContentType = "text/html";
//"application/x-www-form-urlencoded";
if (ProxyServer.Length > 0)
{
webrequest.Proxy = new
WebProxy(ProxyServer,ProxyPort);
}
webrequest.AllowAutoRedirect = false;
if (NwCred)
{
CredentialCache wrCache =
new CredentialCache();
wrCache.Add(new Uri(uri),"Basic",
new NetworkCredential(UserName,UserPwd));
webrequest.Credentials = wrCache;
}
//Remove collection elements
collHeader.Clear();
return webrequest;
}//End of secure CreateWebRequest
代码来自
How to use HttpWebRequest and HttpWebResponse in .NET
http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequest-and-HttpWebResponse-in-N