期望在尝试更新推特状态时失败

时间:2009-07-27 06:21:24

标签: c# .net twitter oauth dotnetopenauth

我似乎无法想出这个。无论我做什么,我都会收到“417 Expectation failed”错误。我看过的每个地方都说我需要摆脱HttpWebRequest的Expect标头。在Web请求ServicePointManager.Expect100Continue = false上设置静态属性request.ServicePoint.Expect100Continue = false或实例属性永远不会删除标头。我必须手动将其设置为null才能将其删除。

无论如何,我仍然得到417错误。我错过了什么?

private static readonly MessageReceivingEndpoint UpdateStatusEndpoint 
       = new MessageReceivingEndpoint("http://twitter.com/statuses/update.xml", HttpDeliveryMethods.PostRequest);

public static XDocument UpdateStatus(ConsumerBase twitter, string accessToken, string message)
{
    var data = new Dictionary<string, string>();
    data.Add("status", message);
    ServicePointManager.Expect100Continue = false; //Doesn't work
    HttpWebRequest request = twitter.PrepareAuthorizedRequest(UpdateStatusEndpoint, accessToken, data);

    request.ServicePoint.Expect100Continue = false; //setting here doesn't work either

    //request.Expect is still set at this point unless I explicitly set it to null.

    request.Expect = null;
    var response = twitter.Channel.WebRequestHandler.GetResponse(request); //Throws exception
    return XDocument.Load(XmlReader.Create(response.GetResponseReader()));
}

3 个答案:

答案 0 :(得分:3)

DotNetOpenAuth不直接设置或支持Except100Continue。如果Channel类中有属性,那就太好了。

在致电'PrepareAuthorizedRequest'之前添加此内容:

    ((HttpWebRequest)WebRequest.Create
(UpdateStatusEndpoint.Location)).ServicePoint.Expect100Continue = false;

根据被叫URL设置Except100Continue。因此,您可以在创建连接之前进行设置。您甚至可以在配置文件中全局设置它。

  <system.net>
    <settings>
      <servicePointManager expect100Continue="false" />
    </settings>
  </system.net>

答案 1 :(得分:1)

将期望值设为“期望:没有”..这是我的代码:

var data = new Dictionary();

data.Add("status", status);

HttpWebRequest request = twitter.PrepareAuthorizedRequest(UpdateStatusEndpoint, accessToken, data);

request.Expect = "expect:nothing";

var response = twitter.Channel.WebRequestHandler.GetResponse(request); exception return XDocument.Load(XmlReader.Create(response.GetResponseReader()));

它对我有用......

答案 2 :(得分:0)

LinqToTwitter是一个使用DotNetOpenAuth的Twitter客户端库。我将看到将一个消息发布示例添加到DotNetOpenAuth核心库以用于将来的版本。