-1

运行此代码时出现以下错误:“HttpWebRequest URI 无效,URI 为空”

CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest PreRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ServiceLogin?utm_source=/places&utm_medium=van&utm_campaign=en&continue=http://www.google.com/local/add/businessCenter%3Fservice%3Dlbc%26gl%3DUS%26hl%3Den-US&service=lbc&hl=en-US&gl=US");
PreRequest.CookieContainer = cookieContainer;
PreRequest.AllowAutoRedirect = true;
HttpWebResponse PreResponse = (HttpWebResponse)PreRequest.GetResponse();
Stream PreResponseStream = PreResponse.GetResponseStream();
PreResponse.Cookies = PreRequest.CookieContainer.GetCookies(PreRequest.RequestUri);
Encoding PreEnc = System.Text.Encoding.UTF8;
StreamReader PreResponseStreamReader = new StreamReader(PreResponse.GetResponseStream(), PreEnc, true);
String PreMyHTML = PreResponseStreamReader.ReadToEnd();

String Pattern = "name=\"GALX\"[\\s]*value=\"([a-zA-Z0-9-_\\.]*)\"";
Match MyMatch = Regex.Match(PreMyHTML, Pattern);
String GALX = MyMatch.Groups[1].ToString();

HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ServiceLoginAuth?service=lbc&ltmpl=sso&continue=http%3A%2F%2Fwww.google.com%2Flocal%2Fadd%2FbusinessCenter%3Fservice%3Dlbc%26gl%3DUS%26hl%3Den-US&service=lbc&uilel=3&ltmpl=sso&hl=en_US&ltmpl=sso&GALX=" + GALX + "&Email=" + user + "&Passwd=YOURACCOUNTPASSWORD&rmShown=1&signIn=Sign+in&asts=");
Request.CookieContainer = cookieContainer;
Request.Method = "POST";
Request.Referer = "https://www.google.com/accounts/ServiceLogin?utm_source=/places&utm_medium=van&utm_campaign=en&continue=http://www.google.com/local/add/businessCenter%3Fservice%3Dlbc%26gl%3DUS%26hl%3Den-US&service=lbc&hl=en-US&gl=US";
Request.MaximumAutomaticRedirections = 50;
Request.AllowAutoRedirect = true;
Request.KeepAlive = true;
Request.ContentLength = 0;

// Get the response
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

Stream ResponseStream = Response.GetResponseStream();
Response.Cookies = Request.CookieContainer.GetCookies(Request.RequestUri);

// Read the response from the stream
Encoding Enc = System.Text.Encoding.UTF8;
StreamReader ResponseStreamReader = new StreamReader(Response.GetResponseStream(), Enc, true);

String MyHTML = ResponseStreamReader.ReadToEnd();
//textBox2.Text = MyHTML;

String Pattern2 = "location\\.replace\\(\"(http:\\/\\/www\\.youtube\\.com\\/signin[a-zA-Z0-9\\\\\\?\\%_-]*)\"\\)";
Match MyMatch2 = Regex.Match(MyHTML, Pattern2);
String YTRedirect = MyMatch2.Groups[1].ToString();
String Temp = YTRedirect.Replace("\\x3d", "=");
String Temp2 = Temp.Replace("\\x26", "&");
YTRedirect = Temp2;

HttpWebRequest PosterRequest = (HttpWebRequest)WebRequest.Create(YTRedirect);
PosterRequest.CookieContainer = cookieContainer;
PosterRequest.Method = "GET";
HttpWebResponse PosterResponse = (HttpWebResponse)PosterRequest.GetResponse();
Stream PosterResponseStream = PosterResponse.GetResponseStream();
PosterResponse.Cookies = PosterRequest.CookieContainer.GetCookies(PosterRequest.RequestUri);
Encoding PosterEnc = System.Text.Encoding.UTF8;
StreamReader PosterResponseStreamReader = new StreamReader(PosterResponse.GetResponseStream(), PosterEnc, true);
String PosterMyHTML = PosterResponseStreamReader.ReadToEnd();

HttpWebRequest PostestRequest = (HttpWebRequest)WebRequest.Create(textBox4.Text);
PostestRequest.CookieContainer = cookieContainer;
PostestRequest.Method = "GET";
HttpWebResponse PostestResponse = (HttpWebResponse)PostestRequest.GetResponse();
Stream PostestResponseStream = PostestResponse.GetResponseStream();
PostestResponse.Cookies = PostestRequest.CookieContainer.GetCookies(PostestRequest.RequestUri);
Encoding PostestEnc = System.Text.Encoding.UTF8;
StreamReader PostestResponseStreamReader = new StreamReader(PostestResponse.GetResponseStream(), PostestEnc, true);
String PostestMyHTML = PostestResponseStreamReader.ReadToEnd();

我正在尝试登录并观看视频,看到点赞或发表评论的方式也会很有趣,感谢帮助!

编辑:错误发生在 HttpWebRequest PosterRequest = (HttpWebRequest)WebRequest.Create(YTRedirect);

4

2 回答 2

0

从该代码中很难分辨,但也许请仔细检查以下内容中包含的内容textBox4.Text

HttpWebRequest PostestRequest = (HttpWebRequest)WebRequest.Create(textBox4.Text);

只是在没有堆栈痕迹的情况下在黑暗中拍摄。

于 2012-05-20T22:06:39.577 回答
0

我还没有阅读任何代码,但是从它的声音来看,请确保在 url 之前包含“http://”。

于 2012-05-20T22:13:42.790 回答