开发者

HttpWebRequest work differently in debug/release

Ive a problem with a code because it work differently if I test it in Visual Studio either I test it on my site.

i am trying to do an autologin on an external web site. If I test it from VS, I am correctly redirected on the external site. From my site i obtain i am redirected to "http://www.MySite.com/cgi-bin/wbc_login/...."!!!!!

The code is the following:

private void MioMetodo(String username, String password)
{
    CookieContainer Cookies = new CookieContainer();

    Cookie langCookie = new Cookie("pk_lang", "\"italiano\"", "/");
    langCookie.Domain = "xxx.yy";
    Cookie loginCookie = new Cookie("wc_loginoslimit", "\"" + username + "\"", "/");
    loginCookie.Domain = "xxx.yy";
    Cookie passwordCookie = new Cookie("wc_passwordoslimit", "\"" + password + "\"", "/");
    passwordCookie.Domain = "xxx.yy";

    Cookies.Add(langCookie);
    Cookies.Add(loginCookie);
    Cookies.Add(passwordCookie);

    UTF8Encoding encoding = new UTF8Encoding();
    String postData = "wc_login=" + username + "&wc_password=" + password + "&limit=0&Avanti=Avanti";
    Byte[] data = encoding.GetBytes(postData);

    HttpWebRequest myHttpWebRequest = WebRequest.Create("http://xxx.yy/cgi-bin/wbc_login") as HttpWebRequest;
    myHttpWebRequest.Method = "POST";
    myHttpWebRequest.Referer = "http://xxx.yy/cgi-bin/wbc_login";

    myHttpWebRequest.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
    myHttpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
    myHttpWebRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4");
    myHttpWebRequest.Headers.Add(HttpRequestHeader.CacheControl, "max-age=0");
    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
    myHttpWebRequest.CookieContainer = Cookies;
    myHttpWebRequest.ContentLength = data.Length;

    Stream newStream = myHttpWebRequest.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();

    HttpWebResponse myHttpWebResponse = myHttpWebRequest.GetResponse() as HttpWebResponse;
    StreamReader streamRead =new StreamReader(myHttpWebResponse.GetResponseStr开发者_开发百科eam());
    Response.Write(streamRead.ReadToEnd());

    streamRead.Close();
    myHttpWebResponse.Close();
}

Thanx in advance

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜