加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

使用ASP.NET卷曲请求

发布时间:2020-12-15 23:01:32 所属栏目:asp.Net 来源:网络整理
导读:我已经阅读了Stack上的其他一些帖子,但是我无法让它工作.当我在我的 Windows机器上的git中运行curl命令时,当我将其转换为asp.net时,它工作正常.它不工作: private void BeeBoleRequest() { string url = "https://mycompany.beebole-apps.com/api"; WebRequ
我已经阅读了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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读