错误请求 400 - 对网站的 httpweb 请求

发布于 2024-12-09 03:26:49 字数 1299 浏览 0 评论 0原文

我正在向一个站点发出请求,他们正在使用 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();

请帮助我。

I am making a request to a site, they are using JSON for exchanging data. Hence I made the following request but it is giving an error -The remote server returned an error: (400) Bad Request.

URL3 is a string

    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();

Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文