HttpWebRequest从SharePoint服务器获得的响应与其他所有内容不同

时间:2014-12-19 09:32:49

标签: c# xml iis sharepoint rss

我尝试连接到Sharepoint服务器并在C#中获取RSS提要。这似乎是一个足够简单的任务,但是出于某种原因,当我发送HttpWebRequest时,sharepoint会向我发送一个不同的响应,以便我在浏览器中访问feed。特别是,它返回没有任何s的feed。

发出请求的代码如下:

HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
request.Credentials = CredentialCache.DefaultCredentials; //NTLM

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
    Stream responseStream = response.GetResponseStream();
    // debug: dump the response.
    System.Diagnostics.Debug.WriteLine((new StreamReader(responseStream)).ReadToEnd());
...

在测试此代码时,我在公共RSS源上尝试了它,它运行正常。我启动了wireshark以查看C#发送的请求是否有不同之处,请求和响应如下所示:

GET /Test_Blog_B/_layouts/15/listfeed.aspx?List=%7B5600FFF4-50CD-47C5-BEBC-602874E73748%7D HTTP/1.1
    HTTP/1.1 401 Unauthorized  (text/plain)
GET /Test_Blog_B/_layouts/15/listfeed.aspx?List=%7B5600FFF4-50CD-47C5-BEBC-602874E73748%7D HTTP/1.1 
    HTTP/XML    438 HTTP/1.1 200 OK 
    <?xml version="1.0" encoding="UTF-8"?>
    <!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 12/18/2014 3:50:28 PM -->
    <?xml-stylesheet type="text/xsl" href="/Test_Blog_B/_layouts/15/RssXslt.aspx?List=5600fff4-50cd-47c5-bebc-602874e73748" version="1.0"?>
    <rss version="2.0">
      <channel>
        <title>Blog: Posts</title>
        <link>http://servername/Test_Blog_B/Lists/Posts/AllPosts.aspx</link>
        <description>RSS feed for the Posts list.</description>
        <lastBuildDate>Thu, 18 Dec 2014 15:50:28 GMT</lastBuildDate>
        <generator>Microsoft SharePoint Foundation RSS Generator</generator>
        <ttl>60</ttl>
        <language>en-US</language>
        <image>
          <title>Blog: Posts</title>
          <url>http://servername/Test_Blog_B/_layouts/15/images/siteIcon.png</url>
          <link>http://servername/Test_Blog_B/Lists/Posts/AllPosts.aspx</link>
        </image>
      </channel>
    </rss>

然而,我尝试的其他一切都得到了这个响应(cURL输出,为了简洁而剪掉了一些标题)

* Connected to servername (192.168.170.39) port 80 (#0)
* Server auth using NTLM with user 
> GET /Test_Blog_B/_layouts/15/listfeed.aspx?List=%7B5600FFF4-50CD-47C5-BEBC-602
< HTTP/1.1 401 Unauthorized
* Connection #0 to host servername left intact
* Issue another request to this URL: 'http://servername/Test_Blog_B/_layouts/15/list
feed.aspx?List=%7B5600FFF4-50CD-47C5-BEBC-602874E73748%7D'
* Found bundle for host servername: 0x94eab0
* Re-using existing connection! (#0) with host servername
* Connected to servername (192.168.170.39) port 80 (#0)
* Server auth using NTLM with user ''
> GET /Test_Blog_B/_layouts/15/listfeed.aspx?List=%7B5600FFF4-50CD-47C5-BEBC-602
874E73748%7D HTTP/1.1
< HTTP/1.1 200 OK
< Cache-Control: private
< Content-Type: text/xml; charset=utf-8
< Last-Modified: Thu, 18 Dec 2014 15:13:06 GMT
< ETag: 1;3;5600fff4-50cd-47c5-bebc-602874e73748;635545156307000000;13319
< Server: Microsoft-IIS/8.5
< X-SharePointHealthScore: 0
< X-AspNet-Version: 4.0.30319
< SPRequestGuid: 7b06d79c-dd72-c064-3da9-961822afeee3
< request-id: 7b06d79c-dd72-c064-3da9-961822afeee3
< X-FRAME-OPTIONS: SAMEORIGIN
< SPRequestDuration: 65
< SPIisLatency: 1
< Persistent-Auth: true
< X-Powered-By: ASP.NET
< MicrosoftSharePointTeamServices: 15.0.0.4569
< X-Content-Type-Options: nosniff
< X-MS-InvokeApp: 1; RequireReadOnly
< Date: Thu, 18 Dec 2014 16:28:44 GMT
< Content-Length: 3061
<
´╗┐<?xml version="1.0" encoding="UTF-8"?>
<!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 12/18/2014
 4:28:44 PM -->
<?xml-stylesheet type="text/xsl" href="/Test_Blog_B/_layouts/15/RssXslt.aspx?Lis
t=5600fff4-50cd-47c5-bebc-602874e73748" version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Blog: Posts</title>
    <link>http://servername/Test_Blog_B/Lists/Posts/AllPosts.aspx</link>
    <description>RSS feed for the Posts list.</description>
    <lastBuildDate>Thu, 18 Dec 2014 16:28:44 GMT</lastBuildDate>
    <generator>Microsoft SharePoint Foundation RSS Generator</generator>
    <ttl>60</ttl>
    <language>en-US</language>
    <image>
      <title>Blog: Posts</title>
      <url>http://servername/Test_Blog_B/_layouts/15/images/siteIcon.png</url>
      <link>http://servername/Test_Blog_B/Lists/Posts/AllPosts.aspx</link>
    </image>
    <item>
      <title>Blog</title>
      <link>http://servername/Test_Blog_B/Lists/Posts/ViewPost.aspx?ID=3</link>
      <description><![CDATA[<div><b>Body:</b> <div class="ExternalClass78B241D68
AE340AF8482D70142AE8DB7"><p>Post</p></div></div>
<div><b>Published:</b> 12/18/2014 3:12 PM</div>
]]></description>
      <author></author>
      <pubDate>Thu, 18 Dec 2014 15:13:06 GMT</pubDate>
      <guid isPermaLink="true">http://servername/Test_Blog_B/Lists/Posts/ViewPost.as
px?ID=3</guid>
    </item>
    <item>
      <title>new post</title>
      <link>http://servername/Test_Blog_B/Lists/Posts/ViewPost.aspx?ID=2</link>
      <description><![CDATA[<div><b>Body:</b> <div class="ExternalClass0BD87B46B
E6F481CA042F2BACC15082C"><p>2ÔÇï</p></div></div>
<div><b>Category:</b> <a onclick="OpenPopUpPage('http://servername/Test_Blog_B/_layo
uts/15/listform.aspx?PageType=4&ListId={7C658DFF-4CEC-4934-98C3-14187E2345CA}&ID
=3&RootFolder=*', RefreshPage); return false;" href="http://servername/Test_Blog_B/_
layouts/15/listform.aspx?PageType=4&ListId={7C658DFF-4CEC-4934-98C3-14187E2345CA
}&ID=3&RootFolder=*">Events</a></div>
<div><b>Published:</b> 12/11/2014 4:55 PM</div>
]]></description>
      <author></author>
      <category>Events</category>
      <pubDate>Thu, 11 Dec 2014 16:55:38 GMT</pubDate>
      <guid isPermaLink="true">http://servername/Test_Blog_B/Lists/Posts/ViewPost.as
px?ID=2</guid>
    </item>
    <item>
      <title>Welcome to my blog!</title>
      <link>http://servername/Test_Blog_B/Lists/Posts/ViewPost.aspx?ID=1</link>
      <description><![CDATA[<div><b>Body:</b> <div class="ExternalClass471F21A09
57043359622B8C8ABB138CF"><p>This is where I'll be sharing my thoughts on topics
that matter to me. Who knows... I might even share pictures, videos and links to
 other interesting stuff.</p><p>If I catch your interest, let me hear from you.<
/p></div></div>
<div><b>Published:</b> 12/11/2014 3:04 PM</div>
]]></description>
      <author></author>
      <pubDate>Thu, 11 Dec 2014 15:04:03 GMT</pubDate>
      <guid isPermaLink="true">http://servername/Test_Blog_B/Lists/Posts/ViewPost.as
px?ID=1</guid>
    </item>
  </channel>
</rss>* Connection #0 to host servername left intact

这是一个干净安装Sharepoint 2013的普通SharePoint博客。在浏览器中打开此Feed会产生与CURL相同的结果,因此我无法看到我的C#代码是如何提供不同的东西。

对此有任何帮助表示赞赏,谢谢

1 个答案:

答案 0 :(得分:0)

因此,解决方案是CredentialCache.DefaultCredentials获取运行IIS服务器的凭据,这不是我自己的凭据。给代码我自己的凭证有效并提供预期的输出。