Web API在不同的环境中有不同的响应

时间:2018-06-22 15:36:27

标签: asp.net-mvc angular asp.net-core

当我使用浏览器致电服务或邮递员Postman响应为true时,可以。但是,如果使用代码调用此服务,我将得到不同的结果enter image description here

方式不一样吗?

以下是位于斜边的代码

export class DataService {

  constructor(private http: HttpClient) { }

  authentificateUser(email, password) {
    return this.http.get('/api/LogIn/Authentification?email=' + email + '&password=' + password).source;
  }
}

控制器

[HttpGet("[action]")]
public bool Authentification(string email, string password)
{
    var user = new SystemUser();
    if (user.EmailAddress == email && user.Password == password)
        return true;
    else return false;
}

我使用ASP.NET核心

2 个答案:

答案 0 :(得分:0)

http.get()。source是做什么的?

您是否尝试过使用Observables?

06-22 16:23:20.367 23735-23735/com.sentilant.driviantasks W/System.err: java.lang.RuntimeException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[27,45]
06-22 16:23:20.368 23735-23735/com.sentilant.driviantasks W/System.err: Message: could not resolve entity named 'nbsp'
        at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:44)
        at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:23)
06-22 16:23:20.369 23735-23735/com.sentilant.driviantasks W/System.err:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)
        at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)
        at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:112)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:141)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
06-22 16:23:20.370 23735-23735/com.sentilant.driviantasks W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
06-22 16:23:20.371 23735-23735/com.sentilant.driviantasks W/System.err: Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[27,45]
    Message: could not resolve entity named 'nbsp'
        at com.bea.xml.stream.MXParser.nextImpl(MXParser.java:1856)
        at com.bea.xml.stream.MXParser.next(MXParser.java:1333)
        at com.bea.xml.stream.XMLEventReaderBase.parseSome(XMLEventReaderBase.java:200)
06-22 16:23:20.372 23735-23735/com.sentilant.driviantasks W/System.err:     at com.bea.xml.stream.XMLEventReaderBase.nextEvent(XMLEventReaderBase.java:126)
        at org.simpleframework.xml.stream.StreamReader.read(StreamReader.java:110)
        at org.simpleframework.xml.stream.StreamReader.next(StreamReader.java:94)
        at org.simpleframework.xml.stream.NodeReader.readElement(NodeReader.java:121)
        at org.simpleframework.xml.stream.NodeReader.skipElement(NodeReader.java:337)
06-22 16:23:20.373 23735-23735/com.sentilant.driviantasks W/System.err:     at org.simpleframework.xml.stream.InputElement.skip(InputElement.java:245)
        at org.simpleframework.xml.core.Composite.readElement(Composite.java:529)
        at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
        at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
        at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
06-22 16:23:20.374 23735-23735/com.sentilant.driviantasks W/System.err:     at org.simpleframework.xml.core.Composite.read(Composite.java:201)
        at org.simpleframework.xml.core.Composite.read(Composite.java:148)
        at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
        at org.simpleframework.xml.core.Persister.read(Persister.java:625)
06-22 16:23:20.375 23735-23735/com.sentilant.driviantasks W/System.err:     at org.simpleframework.xml.core.Persister.read(Persister.java:606)
        at org.simpleframework.xml.core.Persister.read(Persister.java:584)
        at org.simpleframework.xml.core.Persister.read(Persister.java:562)
        at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:36)
        ... 9 more

答案 1 :(得分:0)

在新的HttpClient中,JSON被假定为默认响应。这就是为什么您在控制台中看到对象的原因。

如果要请求非JSON数据,则需要使用responseType选项明确地告诉它。
修改代码:

 return this.http.get('/api/LogIn/Authentification?email=' + email + '&password=' +
 password,{responseType: 'text'})