在我的代码隐藏文件中,我调用了这个函数:
private void loginAction(object sender, TappedRoutedEventArgs e)
{
Webservice webservice = new Webservice();
webservice.getUser(txtLogin.Text, txtPass.Text);
}
然后在网络服务中我这样做:
public void getUser(String user, String password)
{
String strUrl = String.Format("http://*******/nl/webservice/abc123/members/login?email={0}&password={1}",user,password);
startWebRequest(strUrl, loginCallback);
}
private async void loginCallback(IAsyncResult asyncResult)
{
try
{
ReceiveContext received = GetReceiveContextFromAsyncResult(asyncResult, "User");
if (received != null && received.Status == 200)
{
await UserDataSource.AddUser(received.Data);
}
else
{
throw new Exception("failedddd");
}
}
catch (Exception e)
{
}
}
我现在要做的是当抛出异常时,显示一个消息框。当我获得状态 200 时,我导航到代码隐藏文件中的下一页。
我现在的问题是,我如何在我的代码隐藏文件中知道这一点?
非常感谢!
编辑 我还有这些辅助方法:
#region HELPERS
private void startWebRequest(string url, AsyncCallback callback)
{
// HttpWebRequest.RegisterPrefix("http://",WebRequestCreator.ClientHttp);
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
// start the stream immediately
httpWebRequest.AllowReadStreamBuffering = false;
// asynchronously get a response
httpWebRequest.BeginGetResponse(callback, httpWebRequest);
}
private ReceiveContext GetReceiveContextFromAsyncResult(IAsyncResult asyncResult, string context)
{
// Request afleiding van de AsyncState uit het ontvangen AsyncResult
HttpWebRequest httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse httpWebResponse = null;
try
{
// Response afleiden uit de Resuest via de methode EndGetResponse();
httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult);
string responseString;
// using == IDisposable (automatische GC)
using (StreamReader readStream = new StreamReader(httpWebResponse.GetResponseStream()))
{ //Stream van de response gebruiken om een readstream te maken.
responseString = readStream.ReadToEnd();
}
// Release the HttpWebResponse
//httpWebResponse.Dispose();
return new ReceiveContext(int.Parse(responseString.Substring(10, 3)), responseString);
}
catch (WebException wex)
{
Debug.WriteLine(String.Format("{0} kon niet opgehaald worden: {1}", context, wex.Message));
}
catch (JsonReaderException jrex)
{
Debug.WriteLine(String.Format("{0} opgehaald van server, maar de json kon niet geparsed worden: {1}", context, jrex.Message));
}
catch (FormatException fex)
{
Debug.WriteLine(String.Format("{0} opgehaald van server, maar de gegevens kloppen niet: {1}", context, fex.Message));
}
catch (ArgumentOutOfRangeException arex)
{
Debug.WriteLine(String.Format("{0} opgehaald van server, maar de context is leeg: {1}", context, arex.Message));
}
return null;
}
public sealed class ReceiveContext
{
public ReceiveContext(int status, string data)
{
this.Status = status;
this.Data = data;
}
public int Status { get; private set; }
public string Data { get; private set; }
}
#endregion
最佳答案
下面的代码使用 API Windows.Web.Http.HttpClient (微软推荐使用此API连接REST服务)
public class Result
{
public HttpStatusCode Status { get; set; }
public string Data { get; set; }
}
public async Task<Result> LoginAsync(string user, string password)
{
var http = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://*******/nl/webservice/abc123/members/login?email="+ user + "&password="+ password));
var result = await http.SendRequestAsync(request);
var data = new Result {Status = result.StatusCode};
if (result.StatusCode== HttpStatusCode.Ok && result.Content!=null)
{
data.Data = await result.Content.ReadAsStringAsync();
}
return data;
}
在你后面的代码中:
var result2 = await LoginAsync("", "");
if (result2.Status == HttpStatusCode.Ok)
{
//Status code 200
//navigate to other page
}
else if(result2.Status == HttpStatusCode.BadRequest)
{
//Status code 400
//your code
}
关于c# - 在 JSON 中获得 400 webservice 状态时返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30055363/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
所以我开始关注ruby,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha