2012-09-15 100 views
2

这里是发布到craigslist的示例。 http://www.craigslist.org/about/bulk_posting_interface以perl语言。从asp.net网站发布项目到craigslist

在asp.net中的任何craigslist发布示例?

我将perl示例(从上面的链接)转换为asp.net,但仍然生成一个错误,错误是“服务器提交违反协议。部分= ResponseStatusLine” 任何人都有这方面的想法。在此先感谢 这里是我的代码,

string xml = @"<?xml version='1.0'?><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='NYCBrokerHousingSample1'/><rdf:li rdf:resource='NYCBrokerHousingSample2'/> </items> <cl:auth username='[email protected]' password='*******' accountID='14'/> </channel> <item rdf:about='NYCBrokerHousingSample1'> <cl:category>fee</cl:category>  <cl:area>nyc</cl:area> <cl:subarea>mnh</cl:subarea> <cl:neighborhood>Upper West Side</cl:neighborhood> <cl:housingInfo price='1450' bedrooms='0'      sqft='600'/> <cl:replyEmail privacy='C'>[email protected]</cl:replyEmail> <cl:brokerInfo companyName='Joe Sample and Associates' feeDisclosure='fee disclosure here' /> <title>Spacious Sunny Studio in Upper West Side</title> <description><![CDATA[ posting body here ]]></description> </item> </rdf:RDF>";  



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

    request.ProtocolVersion = System.Net.HttpVersion.Version10; 
    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); 
    } 
    return result; 

} // end HttpPost 

回答

0

可以使用Fiddle2(Windows)或查尔斯(OS X)来捕获HTTP请求和响应,比较perl的例子,asp.net例子之间的差异,那么你可能会发现'错误'。

+0

我用Fiddle2和调试但是那产生错误的回应 –

0

我解决此得到了通过添加

request.ProtocolVersion = System.Net.HttpVersion.Version10;