使用 ASP.NET 卷曲请求 [英] curl Request with ASP.NET

查看:24
本文介绍了使用 ASP.NET 卷曲请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于 Stack 的其他帖子,但我无法让它工作.当我在 Windows 机器上的 git 中运行 curl 命令时,它可以正常工作,但是当我将其转换为 asp.net 时,它无法正常工作:

 private void BeeBoleRequest(){字符串 url = "https://mycompany.beebole-apps.com/api";WebRequest myReq = WebRequest.Create(url);字符串用户名 = "e26f3a722f46996d77dd78c5dbe82f15298a6385";字符串密码 = "x";字符串 usernamePassword = 用户名 + ":" + 密码;CredentialCache mycache = new CredentialCache();mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));myReq.Credentials = mycache;myReq.Headers.Add("授权", "基本" + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));WebResponse wr = myReq.GetResponse();流接收流 = wr.GetResponseStream();StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);字符串内容 = reader.ReadToEnd();Response.Write(内容);}

这是 BeeBole API.它非常直接.http://beebole.com/api 但是当我运行上面的代码时出现以下 500 错误:

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

解决方案

WebRequest 的默认 HTTP 方法是 GET.尝试将其设置为 POST,因为这是 API 所期望的

myReq.Method = "POST";

我假设你在发布一些东西.作为测试,我将从他们的 curl 示例中发布相同的数据.

string url = "https://YOUR_COMPANY_HERE.beebole-apps.com/api";字符串数据 = "{"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 = "你的 API 令牌在这里" + ":" + "x";UTF8Encoding enc = new UTF8Encoding();myReq.Headers.Add("授权", "基本" + Convert.ToBase64String(enc.GetBytes(usernamePassword)));使用 (Stream ds = myReq.GetRequestStream()){ds.Write(enc.GetBytes(data), 0, data.Length);}WebResponse wr = myReq.GetResponse();流接收流 = wr.GetResponseStream();StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);字符串内容 = reader.ReadToEnd();Response.Write(内容);

I have read some other posts on Stack but I can't get this to work. It works fine on my when I run the curl command in git on my windows machine but when I convert it to asp.net it's not working:

 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);
    }

This is the BeeBole API. Its pretty straight fwd. http://beebole.com/api but I am getting a following 500 error when I run the above:

The remote server returned an error: (500) Internal Server Error.

解决方案

The default HTTP method for WebRequest is GET. Try setting it to POST, as that's what the API is expecting

myReq.Method = "POST";

I assume you are posting something. As a test, I'm going to post the same data from their curl example.

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), 0, data.Length); 
}


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

这篇关于使用 ASP.NET 卷曲请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆