【问题标题】:HttpWebRequest causes Response does not start with HTTP.DataHttpWebRequest 导致响应不以 HTTP.Data 开头
【发布时间】:2012-12-27 16:07:52
【问题描述】:

我正在尝试通过执行以下操作来使用 craigslist 批量发布:

HttpWebRequest request = null;
Uri uri = new Uri("https://post.craigslist.org/bulk-rss/post");
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = FormattedXmlDocument.InnerXml.Length;
using (Stream writeStream = request.GetRequestStream())
{
    UTF8Encoding encoding = new UTF8Encoding();
    byte[] bytes = encoding.GetBytes(FormattedXmlDocument.InnerXml);
    writeStream.Write(bytes, 0, bytes.Length);
}
string result = string.Empty;

request.ProtocolVersion = System.Net.HttpVersion.Version11;
request.KeepAlive = false;
try
{
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            using (System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8))
            {
                result = readStream.ReadToEnd();
            }
        }
    }
}
catch (Exception exp)
{
    // MessageBox.Show(exp.Message);
}

当这行代码执行时

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

我得到了例外:

The remote server returned an error: (500) Internal Server Error.

我使用 fiddler 来检查请求,它在那里得到以下错误:

Response does not start with HTTP.Data:

包装看起来像这样:

POST https://post.craigslist.org/bulk-rss/post HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: post.craigslist.org
Content-Length: 739
Expect: 100-continue
Connection: Keep-Alive

<rdf:RDF xmlns="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cl="http://www.craigslist.org/about/cl-bulk-ns/1.0"><channel><items><rdf:li rdf:resource="TestJobPost1" /></items><cl:auth username="cl@flazingo.com" password="2749saturn" accountID="14" /></channel><item rdf:about="TestJobPost1"><cl:category>sof</cl:category><cl:area>nyc</cl:area><cl:subarea>stn</cl:subarea><cl:neighborhood>Grasmere</cl:neighborhood><cl:jobInfo compensation="100000.00" telecommuting="0" partTime="0" contract="0" nonprofit="0" internship="0" disability="0" recruitersOK="0" phoneCallsOK="0" okToContact="0" okToRepost="0" /><title>First Position</title><description><![CDATA[teset]]></description></item></rdf:RDF>

任何帮助将不胜感激。

【问题讨论】:

  • FormattedXmlDocument.InnerXml.Length 错误;你需要打电话给encoding.GetByteCount()
  • 更改为 'encoding.GetByteCount(FormattedXmlDocument.InnerXml);'但仍然得到相同的结果。
  • 尝试移动 request.ProtocolVersion = System.Net.HttpVersion.Version11; request.KeepAlive = false;以上GetRequestStream()
  • 你实际上想发布什么?
  • 如果您在“并且包看起来像这样:”下查看我的问题:craigslist.org/about/bulk_posting_interface

标签: c# .net httpwebrequest fiddler


【解决方案1】:

写入request.GetRequestStream() 将开始向服务器发送数据。

之后设置请求属性会破坏协议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多