通过 C# 应用程序在 phpBB Board 中发帖

发布于 2024-08-11 17:46:06 字数 72 浏览 11 评论 0原文

我将改进我的一个新项目,我想添加的功能之一是可以在 phpBB 论坛板上发布新线程,但可以这样做吗?如果是的话,我该怎么做?谢谢。

I'm going to improve one of my new projects and one of the features that I want to add is the possibility to post a new thread in a phpBB forum board, but it's possible to do this? If it is, how can I do this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑疤怪 2024-08-18 17:46:06
  1. 您可以通过向 phpBB 数据库中添加一个简单的插入语句来完成此操作,但是,为了确保一切顺利进行,您需要确保还插入 phpBB 将为新线程插入的任何其他行(查找文档/源代码)。

    此外,您需要确保输入正确的 ID,因为插入中需要唯一的 ID(例如创建线程的用户的 UserID)

  2. 另一种方法是创建一个单独的 php 文件,该文件公开phpBB 用于创建新线程的创建线程函数(w/e 可能会被调用)。您将允许对 php 文件进行 POST/GET(POST 更安全),然后从 C# 运行 HTTP POST/GET 请求。

    在您的新 php 文件中,需要某种类型的授权以确保没有其他人发布/请求该页面。您可以对必须包含特定访问密钥的特定字段名称进行硬编码,以便忽略任何不包含该访问密钥的传入帖子/获取。

第二种方法,我认为,更好,因为它让 phpBB 完成所有艰苦的工作,您只需要正确连接它即可。

然而,使用第二种方法,您可能会遇到安全问题,并且 phpBB 甚至可能不允许您尝试执行的操作。 (我认为要调用某些方法,页面需要有 DEFINE("IN", "PHPBB") 或类似的东西,这对你可以做的事情施加了更多限制。

首先,我会查看 phpBB 支持网站看看第二部分是否可能,并弄清楚调用函数是否可以轻松完成。

  1. You can accomplish this with a simple insert statement into the phpBB database, however, in order to ensure that all things go smoothly, you need to make sure that you also insert any other rows phpBB would otherwise insert for a new thread (look up documentation/source code for that).

    In addition, you would need to make sure you entered the proper IDs for w/e unique IDs are required in the insert (like UserID for the user creating the thread)

  2. Another method is to create a separate php file that exposes the create thread function (w/e it may be called) that phpBB uses to create the new thread. You would allow POST/GET (POST is more secure) to the php file and would then run an HTTP POST/GET request from C#.

    In your new php file, would need some type of authorization to ensure that no one else is posting/requesting the page. You can hard code a specific field name that must contain a specific access key, so that any incoming posts/gets that don't have it will be ignored.

The second method, imo, is better because it lets phpBB do all the hard work, you just need to wire it up correctly.

However, with the 2nd method, you might have security issues and phpBB might not even allow what you're trying to do. (I think to call certain methods, the page needs to have DEFINE("IN", "PHPBB") or something of the sort, which places more constraints on what you can do.

To start off, I would look around phpBB support sites and see if the 2nd part is even possible and figure out if calling the functions is something that can easily be done.

萧瑟寒风 2024-08-18 17:46:06

我不会为您编写所有代码,但我可以转储一些我已经构建的运行良好的东西。

一种方法是创建一个 webbrowser 控件,并创建如下内容:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlDocument doc = null;
            doc = webBrowser1.Document;

            //Find login text box and write user name  
            HtmlElement login = doc.GetElementById("username_or_email");
            login.InnerText = this.login;

            //Find password text box and write password
            HtmlElement password = doc.GetElementById("session[password]");
            password.InnerText = this.password;

            // go to the submit button
            webBrowser1.Document.GetElementsByTagName("input")[5].Focus();
            SendKeys.Send("{ENTER}");

        }

另一种方法是使用 http 请求(不太可能与 phpBB 一起可靠地工作)

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(twitterUrl + userID + ".xml");
                string Credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(this.login + ":" + this.password));

                request.Method = "POST";
                request.ContentType = "application/xml";
                request.AllowWriteStreamBuffering = true;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB6; SLCC1; .NET CLR 2.0.50727;";
                request.Headers.Add("Authorization", "Basic " + Credentials);

                HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();

                string response = HttpWResp.StatusCode.ToString();
                    HttpWResp.InitializeLifetimeService();
                    HttpWResp.Close();    

                return response;

上面的代码用于登录 twitter。您可以修改其中任何一个以满足您的口味。请记住,phpBB 可能会使用验证码和会话验证来阻止您尝试执行的操作。

I wont write all the code for you, but I can dump a couple of things I've built that work well.

One way is to create a webbrowser control, and create something like this:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlDocument doc = null;
            doc = webBrowser1.Document;

            //Find login text box and write user name  
            HtmlElement login = doc.GetElementById("username_or_email");
            login.InnerText = this.login;

            //Find password text box and write password
            HtmlElement password = doc.GetElementById("session[password]");
            password.InnerText = this.password;

            // go to the submit button
            webBrowser1.Document.GetElementsByTagName("input")[5].Focus();
            SendKeys.Send("{ENTER}");

        }

Another way is to use http requests (not as likely to work reliably with phpBB)

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(twitterUrl + userID + ".xml");
                string Credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(this.login + ":" + this.password));

                request.Method = "POST";
                request.ContentType = "application/xml";
                request.AllowWriteStreamBuffering = true;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB6; SLCC1; .NET CLR 2.0.50727;";
                request.Headers.Add("Authorization", "Basic " + Credentials);

                HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();

                string response = HttpWResp.StatusCode.ToString();
                    HttpWResp.InitializeLifetimeService();
                    HttpWResp.Close();    

                return response;

The code above is used to log into twitter. You can modify either of those to suit your tastes. Remember phpBB is likely to use captcha and session validation to prevent exactly what you're trying to do.

余生再见 2024-08-18 17:46:06

好吧,登录到 phpBB3 是很简单的部分,这里是我使用的一些代码

  try
            {
                string format= "autologin=1&login=true&username={0}&password={1}";

                byte[] bytes = Encoding.ASCII.GetBytes(string.Format(format, (object)HttpUtility.UrlEncode("USERNAME"), (object)HttpUtility.UrlEncode("PASSWORD")));

              HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://thephpbb3domain/ucp.php?mode=login");
                httpWebRequest.CookieContainer = new CookieContainer(128);
                httpWebRequest.Timeout = 10000;
                                    httpWebRequest.AllowAutoRedirect = false;
                httpWebRequest.UserAgent = Resources.WEB_USER_AGENT;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.ContentLength = (long)bytes.Length;
                Stream requestStream = ((WebRequest)httpWebRequest).GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse == null)
                {
                    int num2 = (int)MessageBox.Show(Resources.ERR_MSG_NO_DATA);
                    return;
                }
                else
                {
                    StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                    streamReader.ReadToEnd().Trim();
                    streamReader.Close();

                            IEnumerator enumerator2 = httpWebResponse.Cookies.GetEnumerator();
                            try
                            {
                                while (enumerator2.MoveNext())
                                {
                                    Cookie cookie = (Cookie)enumerator2.Current;
                                    string str = HttpUtility.UrlDecode(cookie.Value);
                                    if (cookie.Name.EndsWith("_k"))
                                    {
                                        if (cookie.Value.Length > 5)
                                        {

                                            break;
                                        }
                                    }
                                    else if (cookie.Name.EndsWith("_data") && !str.Contains("s:6:\"userid\";i:-1;") && str.Contains("s:6:\"userid\";"))
                                    {


                                    }
                                }

                            }
                            finally
                            {
                                IDisposable disposable = enumerator2 as IDisposable;
                                if (disposable != null)
                                    disposable.Dispose();
                            }


                }
            }
            catch (WebException ex)
            {
                int num = (int)MessageBox.Show(ex.Message, "HTTP Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }

这使用了以下核心命名空间

       System.Net
       System.Web

在论坛上发布线程但是已经变成了一个真正的巨大挑战,有人有提示吗?

Well Loggin in to phpBB3 is the easy part here is a bit of code i used

  try
            {
                string format= "autologin=1&login=true&username={0}&password={1}";

                byte[] bytes = Encoding.ASCII.GetBytes(string.Format(format, (object)HttpUtility.UrlEncode("USERNAME"), (object)HttpUtility.UrlEncode("PASSWORD")));

              HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://thephpbb3domain/ucp.php?mode=login");
                httpWebRequest.CookieContainer = new CookieContainer(128);
                httpWebRequest.Timeout = 10000;
                                    httpWebRequest.AllowAutoRedirect = false;
                httpWebRequest.UserAgent = Resources.WEB_USER_AGENT;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.ContentLength = (long)bytes.Length;
                Stream requestStream = ((WebRequest)httpWebRequest).GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse == null)
                {
                    int num2 = (int)MessageBox.Show(Resources.ERR_MSG_NO_DATA);
                    return;
                }
                else
                {
                    StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                    streamReader.ReadToEnd().Trim();
                    streamReader.Close();

                            IEnumerator enumerator2 = httpWebResponse.Cookies.GetEnumerator();
                            try
                            {
                                while (enumerator2.MoveNext())
                                {
                                    Cookie cookie = (Cookie)enumerator2.Current;
                                    string str = HttpUtility.UrlDecode(cookie.Value);
                                    if (cookie.Name.EndsWith("_k"))
                                    {
                                        if (cookie.Value.Length > 5)
                                        {

                                            break;
                                        }
                                    }
                                    else if (cookie.Name.EndsWith("_data") && !str.Contains("s:6:\"userid\";i:-1;") && str.Contains("s:6:\"userid\";"))
                                    {


                                    }
                                }

                            }
                            finally
                            {
                                IDisposable disposable = enumerator2 as IDisposable;
                                if (disposable != null)
                                    disposable.Dispose();
                            }


                }
            }
            catch (WebException ex)
            {
                int num = (int)MessageBox.Show(ex.Message, "HTTP Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }

This used the following core namespaces

       System.Net
       System.Web

Posting a thread to the Forum however has turned to be a realy big challenge anyone having hints?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文