我正在尝试在下面的链接中使用示例 api 调用,请检查链接
http://sendloop.com/help/article/api-001/getting-started
我的帐户是“code5”,所以我尝试了 2 个代码来获取系统日期。
1. 代码
var request = WebRequest.Create("http://code5.sendloop.com/api/v3/System.SystemDate.Get/json");
request.ContentType = "application/json; charset=utf-8";
string text;
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
2.代码
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://code5.sendloop.com/api/v3/System.SystemDate.Get/json");
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = "application/json";
Run Code Online (Sandbox Code Playgroud)
但我不知道我通过上面的代码正确使用了 api 吗?
当我使用上面的代码时,我看不到任何数据或任何东西。
我如何获取 api 并将其发布到 Sendloop。我如何通过使用 WebRequest 来使用 api?
我将第一次在 .net 中使用 api 所以
任何帮助将不胜感激。
谢谢。
当我在.NET 4.6.2上运行以下代码时,
var req = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
req.Method = "POST";
req.ContentType = "application/json";
req.Headers.Add("Authorization", "key=my real key is here");
string json = "{\r\n \"to\": \"/topics/news\",\r\n \"data\": {\r\n \"item0\": \"0\",\r\n \"item1\": \"1\"\r\n }\r\n}";
var jsonbytes = Encoding.UTF8.GetBytes(json);
var reqStr = req.GetRequestStream();
reqStr.Write(jsonbytes, 0, jsonbytes.Length);
reqStr.Close();
var res = (HttpWebResponse)req.GetResponse();
Debug.WriteLine(res.StatusCode);
var sr = new StreamReader(res.GetResponseStream());
Debug.Write(sr.ReadToEnd());
sr.Close();
Run Code Online (Sandbox Code Playgroud)
以下异常发生在var res = (HttpWebResponse)req.GetResponse();.
Exception Exception thrown: 'System.ArgumentNullException' in mscorlib.dll ("Value cannot be null.") System.ArgumentNullException
Run Code Online (Sandbox Code Playgroud)
我无法获得更多信息.我不知道什么值是null.我在.NET 4.5.2上运行了相同的代码,没有发生异常.为什么会发生这种异常?
我正在尝试与 mailchimp 同步联系人。当我在联系方式中扩展了 ascii 字符(例如 (Johéctíóú Diamond))时,它会出现以下错误。
远程服务器返回错误:(400) 错误请求。在 System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request) at System.Net.WebClient.UploadString(Uri address, String method, String data) at DealCloud.WinServices.MailChimp.MailChimpHelper。 AddListMembersImpl(MailChimpServiceContext context, String listId, List`1 toAdd)
下面是发送json数据的代码。
internal virtual string AddListMembersImpl(MailChimpServiceContext context, string listId, List<MailChimpMember> toAdd)
{
if (!(toAdd?.Any() ?? false)) return null;
var uri = string.Format("{0}/batches", context.Url/*, listId*/);
using (var webClient = new WebClient())
{
webClient.Headers.Add("Accept", "application/json");
webClient.Headers.Add("Authorization", "apikey " + context.ApiKey);
//var path = $"lists/{listId}/members";
var toSer = new { operations = …Run Code Online (Sandbox Code Playgroud) 上传表格:
<form asp-action="Upload" asp-controller="Uploads" enctype="multipart/form-data">
<input type="file" name="file" maxlength="64" />
<button type="submit">Upload</button>
Run Code Online (Sandbox Code Playgroud)
控制器/文件上传:
public void Upload(IFormFile file){
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("xxxx", "xxxx");
client.UploadFile("ftp://xxxx.xxxx.net.uk/web/wwwroot/images/", "STOR", file.FileName);
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
出现错误“找不到文件 xxxx”。我知道问题在于它试图在"C:\path-to-vs-files\examplePhoto.jpg"FTP 服务器上查找该文件,但该文件显然不存在。我一直在这里查看许多问题/答案,我认为我需要某种FileStream读/写代码。但我目前还没有完全理解这个过程。
我正在尝试从 Amazon 下载 html 文档,但由于某种原因,我得到了一个错误的编码字符串,例如“??K??g??g?e”。
这是我试过的代码:
using (var webClient = new System.Net.WebClient())
{
var url = "https://www.amazon.com/dp/B07H256MBK/";
webClient.Encoding = Encoding.UTF8;
var result = webClient.DownloadString(url);
}
Run Code Online (Sandbox Code Playgroud)
使用 HttpClient 时也会发生同样的事情:
var url = "https://www.amazon.com/dp/B07H256MBK/";
var httpclient = new HttpClient();
var html = await httpclient.GetStringAsync(url);
Run Code Online (Sandbox Code Playgroud)
我还尝试以字节读取结果,然后将其转换回 UTF-8,但我仍然得到相同的结果。另请注意,这并不总是发生。例如,昨天我运行这段代码大约 2 个小时,我得到了一个正确编码的 HTML 文档。但是今天我总是得到一个糟糕的编码结果。它每隔一天发生一次,所以它不是一次性的。
================================================== ================
但是,当我使用 HtmlAgilitypack 的包装器时,它每次都按预期工作:
var url = "https://www.amazon.com/dp/B07H256MBK/";
HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument doc = htmlWeb.Load(url);
Run Code Online (Sandbox Code Playgroud)
即使我明确定义了正确的编码,是什么导致 WebClient 和 HttpClient 得到错误的编码字符串?默认情况下,HtmlAgilityPack 的包装器是如何工作的?
谢谢你的帮助!
为什么这个 C# 代码可以工作,但不是等效的 F# 版本(通过 F# 交互运行)?结果应该是一个只有数字的 HTML 表格。
C# 版本:
var url = "https://www.investing.com/instruments/HistoricalDataAjax";
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.Headers["Origin"] = "http://www.investing.com";
httpRequest.Headers["X-Requested-With"] = "XMLHttpRequest";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36";
var data = "curr_id=1073048&smlID=2627117&header=SE0010296574%20Historical%20Data&st_date=04%2F12%2F2020&end_date=05%2F12%2F2021&interval_sec=Daily&sort_col=date&sort_ord=DESC&action=historical_data";
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(data);
}
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
Console.WriteLine(httpResponse.StatusCode);
Run Code Online (Sandbox Code Playgroud)
F# 版本:
let url …Run Code Online (Sandbox Code Playgroud) 我有一个WCF服务,需要由第三方应用程序调用,发布一些原始XML.
我试图通过构建一个简单的WebRequest并向服务发出请求来测试我的服务.
这是我的服务代码:
接口:
[ServiceContract(Namespace = "http://test.mydomain.com")]
public interface ITest
{
[WebInvoke(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, Method="POST")]
[OperationContract]
Stream SaveXML(Stream input);
}
Run Code Online (Sandbox Code Playgroud)
服务:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(Namespace = "http://test.mydomain.com")]
public class Test : ITest
{
public Stream SaveXML(Stream input)
{
StreamReader streamReader = new StreamReader(input);
string rawString = streamReader.ReadToEnd();
streamReader.Dispose();
// here need to save the input stream to xml format file
Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
byte[] returnBytes = encoding.GetBytes(rawString);
return new MemoryStream(returnBytes);
}
}
Run Code Online (Sandbox Code Playgroud)
配置: …
我想知道是否有可能使用我自己的其他网站进行交互.
这是场景:
假设我有一个Lockerz账户,这是一个你可以做日常工作以赚取积分的地方.每月一次,您可以兑换这些积分以获得奖品,例如ipod,macbook或其他物品.我知道这听起来很荒谬,但和我在一起.
要获得此网站会员资格,必须由会员邀请.所以我收到你的电子邮件地址,然后登录我的帐户,然后从那里发送邀请.
我想要做的是创建一个网站,用户将其电子邮件输入文本框并按下提交按钮.从那里,程序在幕后将我的登录信息和用户电子邮件地址发送给lockerz并发送邀请.一切都没有离开我的网站.
我已经使用VB代码隐藏了一段时间,所以我理解了它的基础知识.我只是想知道我想做什么是可能的.如果是这样,有人可以将我重定向到某种教程或指南,这将给我一个基本的知识.
谢谢
我必须保留2个urllib2开启器,一个用于直接请求,第二个用于通过代理服务器发出请求,我将重建开启器在请求之间切换.
如何分别保持上下文开启,例如直接和代理?
我有一个我需要使用的web api.这个api将根据我发送的内容类型返回数据.通常它会返回html响应.如果请求具有带有'application/json'标头的Content-Type标头,则它将返回json响应.
当我使用Postman尝试并添加内容类型标题(作为application/json)时,一切都很好.但是当使用C#WebRequest对象尝试它时,无论我使用的是内容类型标头,我总是得到html响应.我用fiddler来查看web请求,内容类型总是text/html.
这是我的代码.尝试过这个:
var webAddr = "https://xxxxxxxx.com/api/blabla";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add("Authorization", "Basic xxxxxxxxxxx==");
httpWebRequest.ContentType = "application/json; charset=utf-8";
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
Stream resStream = response.GetResponseStream();
using (var reader = new StreamReader(resStream))
{
result = reader.ReadToEnd();
}
txtResult.Text = result;
Run Code Online (Sandbox Code Playgroud)
还尝试了这个:
var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = "application/json";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
using (var response = (HttpWebResponse)request.GetResponse())
{
var …Run Code Online (Sandbox Code Playgroud)