使用ASP.NET卷曲请求

前端之家收集整理的这篇文章主要介绍了使用ASP.NET卷曲请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经阅读了Stack上的其他一些帖子,但是我无法让它工作.当我在我的 Windows机器上的git中运行curl命令时,当我将其转换为asp.net时,它工作正常.它不工作:
private void BeeBoleRequest()
    {   
        string url = "https://mycompany.beebole-apps.com/api";

        WebRequest myReq = WebRequest.Create(url);            

        string username = "e26f3a722f46996d77dd78c5dbe82f15298a6385";
        string password = "x";
        string usernamePassword = username + ":" + password;
        CredentialCache mycache = new CredentialCache();
        mycache.Add(new Uri(url),"Basic",new NetworkCredential(username,password));
        myReq.Credentials = mycache;
        myReq.Headers.Add("Authorization","Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

        WebResponse wr = myReq.GetResponse();
        Stream receiveStream = wr.GetResponseStream();
        StreamReader reader = new StreamReader(receiveStream,Encoding.UTF8);
        string content = reader.ReadToEnd();
        Response.Write(content);
    }

这是BeeBole API.它挺直的fwd. http://beebole.com/api但是,当我运行以上时,我收到以下500错误

远程服务器返回错误:(500)内部服务器错误.

解决方法

WebRequest的默认HTTP方法是GET.尝试将其设置为POST,因为API正在期待
myReq.Method = "POST";

我假设你发贴了一些东西.作为测试,我将从卷曲示例中发布相同的数据.

string url = "https://YOUR_COMPANY_HERE.beebole-apps.com/api";
string data = "{\"service\":\"absence.list\",\"company_id\":3}";

WebRequest myReq = WebRequest.Create(url);
myReq.Method = "POST";
myReq.ContentLength = data.Length;
myReq.ContentType = "application/json; charset=UTF-8";

string usernamePassword = "YOUR API TOKEN HERE" + ":" + "x";

UTF8Encoding enc = new UTF8Encoding();

myReq.Headers.Add("Authorization","Basic " + Convert.ToBase64String(enc.GetBytes(usernamePassword)));


using (Stream ds = myReq.GetRequestStream())
{
ds.Write(enc.GetBytes(data),data.Length); 
}


WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream,Encoding.UTF8);
string content = reader.ReadToEnd();
Response.Write(content);
原文链接:https://www.f2er.com/aspnet/249885.html

猜你在找的asp.Net相关文章