好心人帮忙,在调用httppost的时候,给我理个思路或者案例代码?
我用C#写了一个调用webapi的项目,webapi因为跨域的问题,我改用在windows的程序实现,调用webapi,也是网上看了一段代码如下:
public static string HttpPost(string url, string body)
{
//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
//request.Headers = "eg-token/5ad67c3b974890798c67c2763b9c532c";
request.ContentType = "application/json";
//application/x-www-form-urlencoded application/json
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
对方提供的两个接口,一个注册,一个登录。注册的接口名称,我用postman测试没问题。需要填写header部分和body部分,我主要想知道,header部分在HttpPost这个方法里怎么填写进去?因为注册之后,对方的webapi会给我产生一个token,这个token的内容有时效性。我可以通过字符串匹配获取这个token内容,但是上面这个里,我如何写进去?因为调用login这个接口了,需要填写header部分,其中要把token写进去。请求各位大侠了