2013-12-23 120 views
0

我似乎无法得到我的HTTP POST方法的挂起。我刚刚学会了如何做GET方法来检索网页,但现在我试图填写网页上的信息,似乎无法让它工作。这回来的源代码始终是一个无效的页面(完全破碎的图像/不正确的信息)带JSON的HTTP帖子

public static void jsonPOST(string url) 
{ 
      url = "http://treasurer.maricopa.gov/Parcel/TaxReceipt.aspx/GetTaxReceipt"; 
      var httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); 
      httpWebRequest.ContentType = "application/json; charset=utf-8"; 
      httpWebRequest.Accept = "application/json, text/javascript, */*; q=0.01"; 
      httpWebRequest.Headers.Add("Accept-Encoding: gzip, deflate"); 
      httpWebRequest.CookieContainer = cookieJar; 
      httpWebRequest.Method = "POST"; 
      httpWebRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); 
      httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW65; Trident/7.0; MAM5; rv:11.0) like Gecko"; 
      httpWebRequest.Referer = "http://treasurer.maricopa.gov/Parcel/TaxReceipt.aspx"; 


      string postData = "{\"startDate\":\"1/1/2013\",\"parcelNumber\":\"17609419\"}"; 
      byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData); 
      httpWebRequest.ContentLength = bytes.Length; 
      System.IO.Stream os = httpWebRequest.GetRequestStream(); 
      os.Write(bytes, 0, bytes.Length); //Push it out there 
      os.Close(); 

      System.Net.WebResponse resp = httpWebRequest.GetResponse(); 
      if (resp == null) 
      { 
       Console.WriteLine("null"); 
      } 
      System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()); 
      string source = sr.ReadToEnd().Trim(); 
} 

编辑:我更新的代码,以反映我的新的问题。我现在的问题是,源代码不是回到我身上的东西。我正在获取source中的原始JSON信息。我可以用它来反序列化我需要获取的信息,但我很好奇为什么实际的源代码不会回到我的身边

+0

这是ASP.NET MVC还是WebForms? – valverij

+0

这是一个控制台应用程序,我试图只为我们的公司数据库的一些信息刮 – MaylorTaylor

回答

0

返回的源代码始终是一个无效页面(完整的破碎图像/没有正确的信息)

这听起来像你只是得到源代码没有考虑相对路径。只要网站上有相对路径,它就不会在你的副本上正确显示。在它有用之前,你必须替换所有的相对路径。

http://webdesign.about.com/od/beginningtutorials/a/aa040502a.htm

记住跨域AJAX可以在这种情况下的一个问题。