我怎样才能“模仿” XMLHTTPRequest的? [英] How can I "Emulate" XMLHTTPRequest?

查看:96
本文介绍了我怎样才能“模仿” XMLHTTPRequest的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最近出现了一个主页,允许您使用一些基本的javascript功能将youtube视频中的注释复制到另一个视频上,我现在正在尝试对此进行扩展并创建更高级的注释创建器,使用C#。



脚本可以在这个网站上喜欢: http://stefansundin.com/stuff/youtube/youtube-copy-annotations.html [ ^ ]但是我正在努力解决的脚本的一部分是:



Hi so I've recently come around a home page that allows you to copy annotations in a youtube video on to another using some base javascript functionality how ever I'm now trying to expand upon this and create a more advance annotation creator, using C#.

The scripts can be fond on this site: http://stefansundin.com/stuff/youtube/youtube-copy-annotations.html[^] but the may part of the script I'm struggling with is:

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('POST', 'https://www.youtube.com/annotations_auth/update2', true);
xhr.send(data);





现在我尝试使用WebRequest和HTTPWebRequest,但它一直在扔我403错误;





For now I've tried using the WebRequest and HTTPWebRequest but it keep throwing me an 403 error;

string xData = xDoc.InnerXml;
            
            byte[] buffer = System.Text.Encoding.ASCII.GetBytes(xData);

            WebRequest rq = (WebRequest)WebRequest.Create("https://www.youtube.com/annotations_auth/update2");
            rq.Method = "POST";
            
            using (Stream s = rq.GetRequestStream())
            {
                s.Write(buffer, 0, buffer.Length);
            }

            HttpWebResponse httpWebResponse = (HttpWebResponse)rq.GetResponse(); //Runtime error The remote server returned an error: (403) Forbidden. 





我已经三倍 - 如果不是四倍 - 检查了XML内容,它应该与我发送的内容完全相同网站,唯一的例外可能是编码?



所以有人知道如何将这个JavaScript变成闪亮的/用C#工作的东西吗?



非常感谢!



I've already triple - if not quadruple - checked the XML content and it should be completely identical with the one I send on the site, where the only exception may be the encoding?

So do anyone know how I can change this JavaScript into something shiny/working in C#?

Thanks a bunch in regard!

推荐答案

(403)禁止



http://en.wikipedia.org/wiki/HTTP_403 [ ^ ]



相关部分:Web服务器可以返回403 Forbidden HTTP状态代码,以响应客户端对网页或资源的请求,以指示可以访问服务器并理解该请求,但拒绝进一步采取行动。状态代码403响应是 Web服务器被配置为拒绝由客户端访问所请求资源 的结果。



我认为javascript正在使用当前的'连接'和经过验证的页面,C#正在创建一个新的未经验证的连接。这是为了防止恶意攻击Web服务器。
(403) Forbidden

http://en.wikipedia.org/wiki/HTTP_403[^]

The relevant portion: A web server may return a 403 Forbidden HTTP status code in response to a request from a client for a web page or resource to indicate that the server can be reached and understood the request, but refuses to take any further action. Status code 403 responses are the result of the web server being configured to deny access, for some reason, to the requested resource by the client.

I think the javascript is using the current 'connection' and the validated page, the C# is creating a new unvalidated connection. It is to prevent malicious hacking of the web server.


这篇关于我怎样才能“模仿” XMLHTTPRequest的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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