HttpWebRequest.GetResponse()错误但与chrome的“PostMan”一起使用(可能的Cookie问题?) [英] HttpWebRequest.GetResponse() errors but works with chrome's "PostMan" (Possible Cookies problem?)

查看:393
本文介绍了HttpWebRequest.GetResponse()错误但与chrome的“PostMan”一起使用(可能的Cookie问题?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,所以我有点尝试过这个问题,但是从来没有真正得到过回答,所以事实是我正在尝试从这个网站上提取功能http://stefansundin.com/stuff/youtube/youtube-copy-annotations.html [ ^ ]进入ac#app然而每当我尝试将XML数据发送到url时它返回一个403 Forbidden。



这个行为让我很困惑,因为如果我使用名为PostMan的Chrome插件一切正常,我每次都会得到200 OK XML也是一样的,网站也可以正常工作。



我已经跟踪了这个问题,可能与cookie有关,但可能无法访问似乎无法解决问题(正如您可以从已注释的行中看到的那样)



我的代码是:(如果有人想要我的XML数据和/或完整代码,请告诉我)



Hey people so I kinda did try to ask this for some ago but never really got answered so the thing is that I'm currently trying to put the functionality from this site http://stefansundin.com/stuff/youtube/youtube-copy-annotations.html[^] into a c# app however every time I try to send the XML data to the url it returns a "403 Forbidden".

This behavior confuses me a lot since if I use the Chrome plugin called "PostMan" everything works and I get 200 OK returned EVERY time and the XML is identical also, as well the site works fine too.

I've tracked the problem to possibly have something to do with cookies not being access probably but I just can't seems to get the problem solved (as you can see from the outcommented lines)

My code is this: (If any one would like my XML data and/or full code please tell me so)

private void PostXML()
{
    byte[] bytes = Encoding.UTF8.GetBytes(richTextBox1.Text.ToString());
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.youtube.com/annotations_auth/update2/");
    //request.KeepAlive = true;
    request.Method = "POST";
    request.AllowAutoRedirect = false;
    CookieContainer cookies = new CookieContainer();
    CookieCollection cookieCollection = cookies.GetCookies(request.RequestUri);
    //cookieGroup.Add(new Cookie("VISITOR_INFO1_LIVE", "l0OlSV7lLCU", "/", ".youtube.com"));
    //cookieGroup.Add(new Cookie("YSC", "7S2fdqmzpq8", "/", ".youtube.com"));
    //cookieGroup.Add(new Cookie("PREF", "f1=50000000", "/", ".youtube.com"));
    //cookieGroup.Add(new Cookie("GEUP", "070e3c5e453bb2e5c5edb10867214dddaQMAAAA=", "/", ".youtube.com"));
    //request.CookieContainer = cookieCollection;
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(bytes, 0, bytes.Length);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //The remote server returned an error: (403) Forbidden.

    if (response.StatusCode != HttpStatusCode.OK)
    {
        string message = String.Format("POST failed. Received HTTP {0}",
        response.StatusCode);
        throw new ApplicationException(message);
    }
}



我在抛出错误时收到此响应头信息(但是set-cookie似乎不时改变):



{X-Frame-Options:SAMEORIGIN

X-Content-Type-Options:nosniff

内容类型:text / html; charset = utf-8

日期:2014年5月15日星期四13:26:37 GMT

到期日:1971年4月27日星期二19:44:06美国东部时间>
Set-Cookie:VISITOR_INFO1_LIVE = 8ccNBaFb8xo;路径= /;域= .youtube.com; expires = Wed,14-Jan-2015 01:19:37 GMT,YSC = ii4JdoC39CY;路径= /;域= .youtube.com;仅Http,PREF = F1 = 50000000;路径= /;域= .youtube.com; expires = Wed,14-Jan-2015 01:19:37 GMT

服务器:gwiseguy / 2.0

P3P:CP =这不是P3P政策!请参阅http ://support.google.com/accounts/bin/answer.py?answer = 151657& hl = da获取更多信息。

缓存控制:无缓存

内容长度:0

X-XSS-Protection:1; mode = block

}



所以我想知道我能做些什么来解决这个问题所以我在我的C#app中可以获得200 OK返回aswel,任何想法?



非常感谢 - Jackie


I'm getting this respond header info back when the error is thrown (However the set-cookie seems to change from time to time):

{X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Date: Thu, 15 May 2014 13:26:37 GMT
Expires: Tue, 27 Apr 1971 19:44:06 EST
Set-Cookie: VISITOR_INFO1_LIVE=8ccNBaFb8xo; path=/; domain=.youtube.com; expires=Wed, 14-Jan-2015 01:19:37 GMT,YSC=ii4JdoC39CY; path=/; domain=.youtube.com; httponly,PREF=f1=50000000; path=/; domain=.youtube.com; expires=Wed, 14-Jan-2015 01:19:37 GMT
Server: gwiseguy/2.0
P3P: CP="This is not a P3P policy! See http://support.google.com/accounts/bin/answer.py?answer=151657&hl=da for more info."
Cache-Control: no-cache
Content-Length: 0
X-XSS-Protection: 1; mode=block
}

So I was wondering what i can do to fix this so I in my C# app can get 200 OK returned aswel, any ideas?

Thanks a lot in regard - Jackie

推荐答案

这篇关于HttpWebRequest.GetResponse()错误但与chrome的“PostMan”一起使用(可能的Cookie问题?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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