远程服务器返回错误:(400)错误请求。 [英] The remote server returned an error: (400) Bad Request.

查看:1645
本文介绍了远程服务器返回错误:(400)错误请求。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的代码出错:远程服务器返回错误:(400)错误请求。生成响应时出错

Why my code gives error :The remote server returned an error: (400) Bad Request. error on generating responce

public void GenerateToken(string sRequestId)
{
    string sRequestContentType = "application/x-www-form-urlencoded,";
    string sBaseUrl = "https://services-sandbox.sheerid.com/rest/0.5/asset/token";
    //string sParameter = "?lifespan=100000" + "&requestId=" + sRequestId;
    string sParameter = "?lifespan=100000&requestId=" + sRequestId;
    sBaseUrl = sBaseUrl + sParameter;       
    string sRes = string.Empty;
    HttpWebRequest oHttpWebRequest = (HttpWebRequest)WebRequest.Create(sBaseUrl);
    oHttpWebRequest.Method = "POST";
    oHttpWebRequest.ContentType = sRequestContentType;
    oHttpWebRequest.UnsafeAuthenticatedConnectionSharing = true;
    oHttpWebRequest.Headers["Authorization"] = "Bearer 624a3eef46f79772958693fa87118b29";
    oHttpWebRequest.Credentials = new NetworkCredential("mrinalkumarjha", "******");    
    // Generating response...    
    HttpWebResponse oHttpWebResponse = (HttpWebResponse)oHttpWebRequest.GetResponse();The remote server returned an error: (400) Bad Request. occur on this line
    
    StreamReader oStreamReader = new StreamReader(oHttpWebResponse.GetResponseStream());
    sRes = oStreamReader.ReadToEnd();    
    ltrToken.Text = sRes ;
}

推荐答案

400 Bad Request 错误是 HTTP 状态代码,表示您发送给网站服务器的请求(例如加载网页的请求)以某种方式格式错误,因此服务器无法理解或处理请求。



检查URL中的错误。 400错误请求错误的最常见原因是因为URL输入错误或者点击的链接指向URL中存在某种错误。



--Amit
The 400 Bad Request error is an HTTP status code that means that the request you sent to the website server (e.g. a request to load a web page) was somehow malformed therefore the server was unable to understand or process the request.

Check for errors in the URL. The most common reason for a 400 Bad Request error is because the URL was typed wrong or the link that was clicked on points to a URL with some kind of mistake in it.

--Amit


我将使用下面的代码,并在浏览器地址栏中测试sBaseUrl,我认为编码需要是encoding.utf8,也许是文件夹令牌没有要附加的查询字符串的默认页面。



I would go with the following below, and test sBaseUrl in the browser address bar, I think it's the encoding needs to be encoding.utf8, and perhaps the folder token doesn't have a default page for the querystring to attach to.

sBaseUrl = httputility.Urlencode(sBaseUrl + sParameter, encoding.utf8);


400错误的身份验证将导致错误的请求异常。

1.检查您要发送的URL是有效的。

2.检查用户名和密码是否写。



注意:检查您是否留下任何空格。复制内容时
400 Bad request exception will cause due to the wrong authentication.
1. check the url which you are sending is valid.
2. check the username and password is write.

note: check whether your leaving any spaces. while copying the contents


这篇关于远程服务器返回错误:(400)错误请求。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆