发新话题
打印

asp.net 使用webrequest登陆LBS类型博客并自动发帖-001jaye

asp.net 使用webrequest登陆LBS类型博客并自动发帖-001jaye

首先对lbs进行分析
  
lbs中一下说明必须先登录某个页面,启动 theCache.genSecurityCode() 否则,在登陆时无法登陆。
//登陆后再获取识别码,我昏
  // theCache.genSecurityCode();
    pageHeader(lang["login"]);
    if(theUser.loggedIn){
      // If user is already logged in
      redirectMessage(lang["error"], lang["login_already"], lang["goback"], "default.asp", false, "errorbox");
    }else{
      var strError=theUser.login(input["username"],input["password"],input["cookiesday"],true,input["scode"]);
      if(strError==false){
        redirectMessage(lang["done"], lang["login_done"], lang["redirect"], strReferer, true, "messagebox");
      }else{
        strError=lang["login_error"]+'<ul id="errorlist">'+strError+"</ul>"
        redirectMessage(lang["error"], strError, lang["goback"], "javascript:window.history.back();", false, "errorbox");
      }
   theCache.genSecurityCode();
    }
//产生识别码的function
// Generate Security Code
  this.genSecurityCode = function(){
    if(Session("lbsSecurityCode")==undefined){
      Session("lbsSecurityCode")=func.randomStr(4,"0123456789");
    }else{
      if(Session("lbsSecurityCode").length!=4) Session("lbsSecurityCode")=func.randomStr(4,"0123456789");
    }
  }
//也就是立即登陆会在此处造成错误
if(!strUsername||!strPassword||(!bNoSCode&&!Session("lbsSecurityCode"))){
     strError+="<li>"+lang["form_incomplete"]+"</li>";
}
个人感觉lbs因为是非商业代码所以,没有非常规范地写代码,当然lbs还是好系统。

解决办法先登录首页初始化session后,再保持住这个session后再登陆,然后再发帖
'设置统一的session
        Dim cookieCon As CookieContainer = New CookieContainer()
        Dim myRequest As HttpWebRequest
        Dim strId As String = TextBox1.Text '用户名
        Dim strPassword As String = TextBox2.Text '密码
        Dim httpstr As String
        Dim encoding As New UTF8Encoding 'lbs不是ASCIIEncoding()
        httpstr = "http://myblog.vlly.net/" '博客地址
        '访问首页以启动session初始
        myRequest = CType(WebRequest.Create(httpstr & "Default.asp"), HttpWebRequest)
        myRequest.CookieContainer = cookieCon
        Dim myResponse As HttpWebResponse = CType(myRequest.GetResponse(), HttpWebResponse)
        '登陆页post
        '<FORM name=Login action=http://myblog.vlly.net/login.asp?act=login method=post>
      
        myRequest = CType(WebRequest.Create(httpstr & "login.asp?act=login"), HttpWebRequest)
        myRequest.CookieContainer = cookieCon 'myrequest赋值后必须重新设置cookie
        myRequest.Method = "POST"
        myRequest.ContentType = "application/x-www-form-urlencoded"
        myRequest.AllowAutoRedirect = False
        Dim postData As String = "username=" + strId
        postData += "&password=" + strPassword
        postData += "&CookiesDay=1"
        ' postData += "&Login=%E7%99%BB%E9%99%86"
        Dim data As Byte() = encoding.GetBytes(postData)
        myRequest.ContentLength = data.Length
        ' Send the data.
        Dim newStream As Stream = myRequest.GetRequestStream()
        newStream.Write(data, 0, data.Length)
        newStream.Close()
        ' Get response
        myResponse = CType(myRequest.GetResponse(), HttpWebResponse)
        Dim reader As New StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8)
        Dim content As String = reader.ReadToEnd()

        '自动发帖
        '<FORM name=inputform action=?act=save method=post>
        myRequest = CType(WebRequest.Create(httpstr & "article.asp?act=save"), HttpWebRequest)
        myRequest.CookieContainer = cookieCon 'myrequest赋值后必须重新设置cookie
        myRequest.Method = "POST"
        myRequest.ContentType = "application/x-www-form-urlencoded"
        myRequest.AllowAutoRedirect = False
        postData = "log_catid=10&log_mode=1&log_title="
        postData += HttpUtility.UrlEncode("标题") & "&log_id=0&log_postTime="
        '  postData += "2007-01-11+13%3A00%3A05" '发帖时间
        postData += HttpUtility.UrlEncode(DateTime.Now.ToString()) '发帖时间
        postData += "&font=&size=&color=&mode=1&"
        postData += "message=" & HttpUtility.UrlEncode("内容") & "&log_trackbackurl="
        Dim dataw As Byte() = encoding.GetBytes(postData)
        myRequest.ContentLength = dataw.Length
        ' Send the data.
        Dim newStreamw As Stream = myRequest.GetRequestStream()
        newStreamw.Write(dataw, 0, dataw.Length)
        newStreamw.Close()
        ' Get response
        myResponse = CType(myRequest.GetResponse(), HttpWebResponse)
        Dim readerw As New StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8)
        Dim contentw As String = readerw.ReadToEnd()
        ContentHtml.Text = contentw

null

TOP

发新话题