Tag: bad request

使用asp.net和C#从javascript调用外部服务器上的webservice

我正在尝试使用ASP.NET页面测试Web服务调用,该页面创建一个包含用户名和密码字段以及“提交”按钮的表单。 (我正在使用的jQuery和.js文件都包含在head元素的脚本标记中。) “提交”按钮调用在C#代码隐藏文件中创建的函数,该函数调用单独的JavaScript文件。 protected void mSubmit_Click(object sender, EventArgs eventArgs) { String authenticate = String.Format(“Authentication(\”{0}\”,\”{1}\”);”, this.mUsername.Text,this.mPassword.Text); Page.ClientScript.RegisterStartupScript(this.GetType(), “ClientScript”, authenticate, true); } JavaScript函数Authenticate使用jQuery和Ajax对不同的服务器进行Web服务调用,发送JSON参数并期望返回JSON作为响应。 function Authentication(uname, pwd) { //gets search parameters and puts them in json format var params = ‘{“Header”:{“AuthToken”:null,”ProductID”:”NOR”,”SessToken”:null,”Version”:1},”ReturnAuthentication”:true,”Password”:”‘ + pwd + ‘”,”Username”:”‘ + uname + ‘”,”ReturnCredentials”:false }’; var xmlhttp = $.ajax({ async: false, type: “POST”, […]

如何在Windows应用程序C#.net中获取Google plus访问令牌

我正在准备一个Windows应用程序,因为我想使用oAuth2来访问来自google plus的信息。 但我对以下代码的要求不好。我可以在谷歌控制台中创建应用程序并获取应用程序访问的“代码”。 WebRequest request = WebRequest.Create( GoogleAuthenticationServer.Description.TokenEndpoint ); // You must use POST for the code exchange. request.Method = “POST”; // Create POST data. string postData = FormPostData(code); byte[] byteArray = Encoding.UTF8.`enter code here`GetBytes(postData); // Set up the POST request for the code exchange. request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); […]

MVC Controller返回错误请求?

我想知道是否有可能通过MVC控制器的内容返回错误的请求? 我能够做到这一点的唯一方法是throw HttpException但是在这里我无法设置任何内容。 试过这种方法,但由于一些奇怪的原因,我总是得到回报。 是否有可能做到这一点? public class SomeController : Controller { [HttpPost] public async Task Foo() { var response = new HttpResponseMessage(HttpStatusCode.BadRequest); response.Content = new StringContent(“Naughty”); return response; } }

MS Azure – 错误请求400

每次运行这行代码时,我都会收到错误的请求错误: List accounts = await App.accountTable.Where(account => account.EmailAddress == email).ToListAsync(); 这是错误消息: A first chance exception of type ‘Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException’ occurred in mscorlib.dll Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The request could not be completed. (Bad Request) at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.d__18.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.d__1d.MoveNext() […]

使用api 1.1的Twitter POST问题

我们刚刚更改为Twitter api 1.1,现在Tweeting不起作用并返回错误“远程服务器返回错误:(400)错误请求。” 关于SO的研究表明它与身份validation有关,但我们正在发送我们刚刚从登录页面获得的accessToken和secret。 这一切都适用于api 1.0。 代码是 – public void Tweet(Action response, string message) { StringBuilder sb = new StringBuilder(); sb.Append(“POST&”); sb.Append(Uri.EscapeDataString(_postUrl)); sb.Append(“&”); string oauthNonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); string timeStamp = MakeTimestamp(); var dict = new SortedDictionary { { “oauth_consumer_key”, _oAuthConfig.ConsumerKey }, { “oauth_nonce”, oauthNonce }, { “oauth_signature_method”, “HMAC-SHA1” }, { “oauth_timestamp”, timeStamp }, { […]

Google Calendar API – 错误请求(400)尝试交换访问令牌代码

获得Google的授权代码以访问用户的日历后,我现在尝试将其替换为访问令牌。 根据自己的文档: 实际请求可能如下所示: POST /o/oauth2/token HTTP/1.1 Host: accounts.google.com Content-Type: application/x-www-form-urlencoded code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu& client_id=8819981768.apps.googleusercontent.com& client_secret={client_secret}& redirect_uri=https://oauth2-login-demo.appspot.com/code& grant_type=authorization_code 我尝试访问它的方法如下(C#): string url = “https://accounts.google.com/o/oauth2/token”; WebRequest request = HttpWebRequest.Create(url); request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded”; string body = “code=&\r\n” + “client_id=&\r\n” + “client_secret=&\r\n” + “redirect_uri=http://localhost:4300\r\n” + “grant_type=authorization_code&\r\n” ; byte[] bodyBytes = Encoding.ASCII.GetBytes(body); request.ContentLength = bodyBytes.Length ; Stream bodyStream = request.GetRequestStream(); […]

C#WebRequest.getResponse():400错误请求

我试图使用System.Web从服务器下载文件。 它确实有效,但有些链接给我带来了麻烦。 链接看起来像这样: http://cdn.somesite.com/r1KH3Z%2FaMY6kLQ9Y4nVxYtlfrcewvKO9HLTCUBjU8IBAYnA3vzE1LGrkqMrR9Nh3jTMVFZzC7mxMBeNK5uY3nx5K0MjUaegM3crVpFNGk6a6TW6NJ3hnlvFuaugE65SQ4yM5754BM%2BLagqYvwvLAhG3DKU9SGUI54UAq3dwMDU%2BMl9lUO18hJF3OtzKiQfrC/the_file.ext 代码看起来基本上是这样的: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link); WebResponse response = request.getResponse(); getResponse()始终抛出exception(错误400错误请求)。 但是,我知道该链接有效,因为我可以使用Firefox下载该文件而不会出现问题。 我还尝试用Uri.UnescapeDataString(链接)解码链接,但该链接甚至不能在Firefox中工作。 其他链接以这种方式完美地工作..只是这些不起作用。 编辑: 好的,我发现了一些使用wireshark的东西: 如果我使用Firefox打开链接,则发送: &ME3@”dM*PNyAo PA:]GET /r1KH3Z%2FaMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs4QgbudzcrJivrAaOTYkEnozqmdoSCCY8yb1i22YtEAV/epd_outpost_12adb.flv HTTP/1.1 Host: cdn.somesite.com User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive 我认为只有第一行是问题,因为WebRequest.Create(link)解码了url: &MEz.@!dM/nP9@~P>.GET /r1KH3Z/aMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs6Mmh1EsQQ4vJVYUwtbLBDNx9AwCHlWDfzfSWIHzaaIo/epd_outpost_12adb.flv HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0 […]