C# 带JSON的HTTP Post
我似乎无法掌握HTTP POST方法的诀窍。我刚刚学会了如何获取检索网页的方法,但现在我试图在网页上填写信息,似乎无法使其正常工作。返回的源代码总是一个无效页面(满是损坏的图像/信息不正确)C# 带JSON的HTTP Post,c#,json,post,C#,Json,Post,我似乎无法掌握HTTP POST方法的诀窍。我刚刚学会了如何获取检索网页的方法,但现在我试图在网页上填写信息,似乎无法使其正常工作。返回的源代码总是一个无效页面(满是损坏的图像/信息不正确) 编辑:我更新了代码以反映我的新问题。我现在的问题是,源代码不是我想要的。我只在源代码中获取原始JSON信息。我可以用它来反序列化我需要获得的信息,但我很好奇为什么实际的源代码没有返回给我返回的源代码总是一个无效页面(满是破碎的图像/不是正确的信息) 听起来你只是得到了源代码,没有考虑到相关的路径。只要站点上
编辑:我更新了代码以反映我的新问题。我现在的问题是,源代码不是我想要的。我只在
源代码中获取原始JSON信息。我可以用它来反序列化我需要获得的信息,但我很好奇为什么实际的源代码没有返回给我返回的源代码总是一个无效页面(满是破碎的图像/不是正确的信息)
听起来你只是得到了源代码,没有考虑到相关的路径。只要站点上有相对路径,它就不会在您的副本上正确显示。必须先替换所有的相对路径,它才有用
请记住,在这种情况下,跨域ajax可能是一个问题。这是ASP.NET MVC还是WebForms?这是一个控制台应用程序,我正试图为我们的公司数据库获取一些信息
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();
}