错误的请求400 - httpwebrequest到一个网站

时间:2011-10-10 20:30:26

标签: asp.net httpwebrequest

我正在向网站发出请求,他们正在使用JSON来交换数据。因此我提出了以下请求,但它发出错误 - 远程服务器返回错误:(400)错误请求。

URL3是一个字符串

    httpWebRequest = (HttpWebRequest)WebRequest.Create(URL3);
    httpWebRequest.Method = "POST";
    httpWebRequest.Host = "url";
    httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML,        like Gecko) Chrome/13.0.782.112 Safari/535.1";
    httpWebRequest.KeepAlive = true;
    httpWebRequest.ContentType = "application/json; charset=UTF-8";
    httpWebRequest.Referer = "url2";
    httpWebRequest.CookieContainer = cookieContainer;
    httpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
    httpWebRequest.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");

    string postData = "";
    postData += "{\"prefixText\":\"hyderabad\",\"count\":10,\"contextKey\":\"45\"}";

    StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
    requestWriter.Write(postData); //posting the data
    requestWriter.Close();

    **httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();**
    responseStreamReader = new StreamReader(httpWebResponse.GetResponseStream());
    string responseData_3 = responseStreamReader.ReadToEnd();
    responseStreamReader.Close();

请帮帮我。

0 个答案:

没有答案