代码之家  ›  专栏  ›  技术社区  ›  Jason

如何通过ASP.NET(最好是VB)在Twitter上发一篇简单的帖子?

  •  4
  • Jason  · 技术社区  · 15 年前

    我不想在Twitter上做任何花哨的事情,除了每天通过我的网站发一次。我搜索了一下,有各种各样的超复杂的方法来做Twitter做的每一件小事,但似乎很少有关于如何做最简单的事情的文档,那就是写一篇文章!

    有人知道怎么做吗?或者你能至少给我指出正确的方向吗?我不需要全套包装纸什么的( http://apiwiki.twitter.com/Libraries#C/NET ,只有一个简单的功能可以发布到Twitter。

    谢谢!

    4 回复  |  直到 12 年前
        1
  •  4
  •   Sled bayer    12 年前

    这是有史以来最简单的实现。在2分钟内启动和运行: Twitterizer

        2
  •  0
  •   u07ch    15 年前

    它相当简单;您只需要使用webrequest.create将XML文件发布到网页。这个例子很接近(假设您在另一个地方有消息的XML,并将其作为字符串传递到twitterxml变量中。URL可能不是正确的;在定义接口的[page][1]上找到它

    WebRequest req = null;
       WebResponse rsp = null;
       try
       {
        string twitterXML = "xml as string";
        string uri = "http://twitter.com/statuses/update.format";
        req = WebRequest.Create(uri);
        //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
        req.Method = "POST";        // Post method
        req.ContentType = "text/xml";     // content type
        // Wrap the request stream with a text-based writer
        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        // Write the XML text into the stream
        writer.WriteLine(twitterXML);
        writer.Close();
        // Send the data to the webserver
        rsp = req.GetResponse();
    
       }
    

    〔1〕: http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses γ更新

        3
  •  0
  •   Community CDub    7 年前

    有两种不同的方法可以做到这一点,这取决于您想要使用和可以访问的工具。选项1将立即生效,但编码可能很复杂。选项3您将不得不下载的工具,但一旦安装和加载,您应该能够非常快地使用TwitterAPI。

    1. 使用webrequest.create创建/向远程端点发送消息
    2. 使用wcf,创建镜像端点,并使用仅客户端端点访问Twitter API。
    3. 使用 WCF REST Starter Kit Preview 2 ,它有一个名为httpclient的新类。如果可以的话,我不得不推荐这种方法。这是一个很棒的视频 Consuming a REST Twitter Feed in under 3 minutes .

    以下是使用wcf rest starter kit的httpclient的示例:

        public void CreateFriendship(string friend)
        {
            using (var client = new HttpClient())
            {
                var url = string.Format("http://www.twitter.com/friendships/create/{0}.xml?follow=true", friend);
                client.Post(url)
                    .CheckForTwitterError()
                    .EnsureStatusIs(HttpStatusCode.OK);
            }
        }
    

    如果您想了解有关特定方法的更多信息,请添加注释。

    更新:

    有关选项1,请参阅以下问题: Remote HTTP Post with C#

        4
  •  0
  •   Sheraz    15 年前

    有几种方法可以做到这一点,您可以查看 http://restfor.me/twitter 它将提供RESTful文档中的代码。

    本质上,进行任何经过身份验证的调用都可以遵循以下逻辑:

    
            /// 
            /// Executes an HTTP POST command and retrives the information.     
            /// This function will automatically include a "source" parameter if the "Source" property is set.
            /// 
            /// The URL to perform the POST operation
            /// The username to use with the request
            /// The password to use with the request
            /// The data to post 
            /// The response of the request, or null if we got 404 or nothing.
            protected string ExecutePostCommand(string url, string userName, string password, string data) {
                WebRequest request = WebRequest.Create(url);
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";
                if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) {
                    request.Credentials = new NetworkCredential(userName, password);
    
    
    
                    byte[] bytes = Encoding.UTF8.GetBytes(data);
    
                    request.ContentLength = bytes.Length;
                    using (Stream requestStream = request.GetRequestStream()) {
                        requestStream.Write(bytes, 0, bytes.Length);
    
                        using (WebResponse response = request.GetResponse()) {
                            using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                                return reader.ReadToEnd();
                            }
                        }
                    }
                }
    
                return null;
            }