从 C# 代码将文件上传到 Google 网站
本文关键字:Google 网站 文件 代码 | 更新日期: 2023-09-27 17:55:15
知道如何从 c# 将文件上传到 Google 网站吗?
我正在尝试上传,但收到 403 错误。但是,我使用相同的凭据连接到站点并获取站点上存在的附件和页面列表。
任何帮助将不胜感激!
他们很可能有一个反CSRF方案,在页面和/或cookie中存储临时标识符,这是专门用来阻止机器人的。您很可能在没有正确 CSRF 令牌的情况下提交请求并被拒绝。我建议分析他们如何处理CSRF,在此之后,它很可能会归结为对页面进行WebRequest,这样您就可以获得他们返回的任何cookie,以及表单,以便您可以删除任何相关的隐藏字段。然后将这些内容移动到您尝试将文件发送到的帖子请求中。
我想出了问题并解决了它。以下是完整的功能:
public bool UploadAttachment()
{
try
{
//AsyncSendData data = new AsyncSendData();
string parentUrl = Cabinets["Cabinet1"].ToString();
string parentID = parentUrl.Split('/')[7];
AtomEntry entry = new AtomEntry();
entry.Title.Text = "abc.jpg";
AtomCategory cat = new AtomCategory();
cat.Term = ATTACHMENT_TERM;
cat.Label = "attachment";
cat.Scheme = KIND_SCHEME;
entry.Categories.Add(cat);
AtomLink link = new AtomLink();
link.Rel = PARENT_REL;
link.HRef = parentUrl;
entry.Links.Add(link);
AtomContent content = new AtomContent();
FileInfo info = new FileInfo("C:''Bluehills.txt");
FileStream stream = info.Open(FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);
this.setUserCredentials(userName, password);
Uri postUri = new Uri(makeFeedUri("content"));
entry.Source = new AtomSource();
//this.EntrySend(postUri, entry, GDataRequestType.Insert);
// Send the request and receive the response:
AtomEntry insertedEntry = this.Insert(postUri, stream, (string)DocumentTypes["TXT"], "bluehills");
return true;
}
catch (Exception ex)
{
return false;
}
}