locked
i got code 200 but nothing really happened - no result on the receiver side ! RRS feed

  • Question

  • User-1312778766 posted

    i got a web form - aspx page i am trying to post parameters to by using api call cross domain.

    this aspx page looking for post parameters on page load.

     

    when i tried the api call  i do get code 200 - but nothing happened...  (this page work ok with js ajax.)

    so: if i get 200 - who to get any clue about the problem... any hint? 

    on the page on the page side

    from the page web.config
    
    <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Headers" value="accept, content-type"/>
            <add name="Access-Control-Allow-Origin" value="*"/>
            <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS"/>
          </customHeaders>
        </httpProtocol>
    protected void Page_Load(object sender, EventArgs e)
        {
    
            //contact[name] = XXXX &
            //contact[address] = x % 40x.com &
            //contact[message] = XXXXXXXXXXX &
            //contact[verification] =
            if (Request.Form.Keys.Count != 4)
            {
                Response.Flush();
                Response.Write("no entry: 9s8fe6s3krhqsruijqop");
                Response.End();
            }
    
            string sSubject = "msg from mySites: " +  Request.UrlReferrer.DnsSafeHost;
    
            string msg = GetEmailBody();
    
            new gmailMe(false).postMe(sSubject, msg);
    
    
        }

    // FROM THE API CALL

    internal async Task<HttpResponseMessage> PostMeAsync(string sName, string sAddress, string sSubject, string sMsg)
            {
                //$.post('http://WWW...'
                //contact[name] = XXXX &
                //contact[address] = x % 40x.com &
                //contact[message] = XXXXXXXXXXX &
                //contact[verification] =
                
                using (HttpClient myHttpClient = new HttpClient())
                {
                    var formContent = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string, string>("name", sSubject),
                        new KeyValuePair<string, string>("address", sAddress),
                        new KeyValuePair<string, string>("message", sMsg),
                        new KeyValuePair<string, string>("verification", sName)
                    });
    
                    formContent.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                    var response = await myHttpClient.PostAsync("http://XZZ/contact-Zs/ZZZ", formContent);
                    return response;
                }
                
            }

    and this is the 200

    {StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
    Cache-Control: private
    Transfer-Encoding: chunked
    Server: Microsoft-IIS/8.5
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Access-Control-Allow-Headers: accept, content-type
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: POST, GET, OPTIONS
    Set-Cookie: ARRAffinity=eeb2d121089141048d51fad182b3860314dcc4edac87e555d4048acf68bb9324;Path=/;Domain=theDomain
    Date: Mon, 29 Oct 2018 12:09:25 GMT
    Content-Type: text/html
    }}

    Monday, October 29, 2018 12:50 PM

All replies

  • User753101303 posted

    Hi,

    Seems you have two problems here. 

    1) What if you return IHttpActionResult and use return ResponseMessageResult(response); You are using Web API 2 ? For now my understanding is that using the XZZ API from js work and that this API call might work well except that there is some confusion between the actual http response and the .NET object that allows to access this response (currently you return this serialized object).

    2) If the XZZ API really fails but returns 200 OK is this under your control ? If not I'm not sure you can do much rather than to report them (unless you think it fails because the result is not what you expected because of #1 ?)

    Monday, October 29, 2018 1:10 PM
  • User-1312778766 posted

    thank you PatriceSc,

    yes this code is under my control..

    in this case on both sides not using web api.... try to post it from xamarin - cross platform to website webForm aspx post[params] on page_load,

    i have other api calls what works.. although posting to a web api domain (different domain from above call) 

    for example

    internal async Task<HttpResponseMessage> LogoutAsync(string accessToken)
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
    
                    //HttpContent content = new StringContent("");
    
                    var responce = await client.PostAsync(GetBaseApiAddress() + __Api_TimeRolls + "/Logout", null);
    
                    return responce;
                }
    
            }

    and so -> i am using system.NET.http

    for using  ResponseMessageResult i will have to add System.WEB.Http reference...

    but.. i am trying to have it light as possible... if possible 

    so first - is it possible to result it with system.NET.http? as the func above...

    could you please try to explain that confusion ...

    Monday, October 29, 2018 2:23 PM
  • User475983607 posted

    You have conceptual and logical issues.  First, CORS is browser security and only applies to requests made from a browser. 

    The content type should be application/x-www-form-urlencoded not text/html

    formContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

    If this is the API endpoint, then the access modifier should be public not internal.

    internal async Task<HttpResponseMessage> PostMeAsync(string sName, string sAddress, string sSubject, string sMsg)
            {
                //$.post('http://WWW...'
                //contact[name] = XXXX &
                //contact[address] = x % 40x.com &
                //contact[message] = XXXXXXXXXXX &
                //contact[verification] =
                
                using (HttpClient myHttpClient = new HttpClient())
                {
                    var formContent = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string, string>("name", sSubject),
                        new KeyValuePair<string, string>("address", sAddress),
                        new KeyValuePair<string, string>("message", sMsg),
                        new KeyValuePair<string, string>("verification", sName)
                    });
    
                    formContent.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                    var response = await myHttpClient.PostAsync("http://XZZ/contact-Zs/ZZZ", formContent);
                    return response;
                }
                
            }

    It look like the ASPX page sends an email.  Simply send the email from Web API rather than making a new call.

    IMHO, you need to rethink or simplify the approach.

    Monday, October 29, 2018 3:00 PM
  • User475983607 posted

    yes this code is under my control..

    in this case on both sides not using web api.... try to post it from xamarin - cross platform to website webForm aspx post[params] on page_load,

    i have other api calls what works.. although posting to a web api domain (different domain from above call) 

    for example

    internal async Task<HttpResponseMessage> LogoutAsync(string accessToken)
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
    
                    //HttpContent content = new StringContent("");
    
                    var responce = await client.PostAsync(GetBaseApiAddress() + __Api_TimeRolls + "/Logout", null);
    
                    return responce;
                }
    
            }

    and so -> i am using system.NET.http

    for using  ResponseMessageResult i will have to add System.WEB.Http reference...

    but.. i am trying to have it light as possible... if possible 

    so first - is it possible to result it with system.NET.http? as the func above...

    could you please try to explain that confusion ...

    If I understand the code, you are trying to logout a user from an intermediate API endpoint?  This is a questionable design which requires that you explain how security works in your application.  If this is a browser based app that use cookie auth, then the design will not work.

    Monday, October 29, 2018 3:09 PM
  • User-1312778766 posted

    yes - not using in here web browser or cookies - its a call from xamarin mobile device using api.

    good idea: i will set the email as an api as well.. (web method) 

    thanks 

    Monday, October 29, 2018 3:33 PM
  • User475983607 posted

    yes - not using in here web browser or cookies - its a call from xamarin mobile device using api.

    good idea: i will set the email as an api as well.. (web method) 

    Then what does the ASPX page have to do with this solution?  ASPX forms are browser based applications.

    Can you explain the design as the code and problem description do not make logical sense.

    Monday, October 29, 2018 3:48 PM
  • User753101303 posted

    Ok so you mean the HttpClient call you shown is inside a Xamarin app ? And what you call is this ASPX page ? And so you are showing us a serialized HttpResponseMessage as a debugging information ?

    Could it be that Request.Form.Keys.Count is not 4 ? You return just a string message not shown in your the HttpResponseMessage and so you should still get a 200 OK if this happen, the only way to see that being to check the actual returned content.

    Rather than using Response.Write, you could throw an exception or maybe better try Respknse.StatusCode=400 (bad request) to tell the incoming request is not what you expect.

    If this problem is confirmed try to use the "application/x-www-form-urlencoded" content type (rather than "text/html").

    It is also possible that technically speaking sending the mail works fine even if it is not finally delivered for some reason.

    As I tell very often you should not try to find out what is wrong  by reading your code but rather by seeing what happens, starting by making 100% sure if you are entering this " if (Request.Form.Keys.Count != 4)" block.

    Monday, October 29, 2018 5:32 PM
  • User-1312778766 posted

    its happened to be many websites working together - multiple solution and multiple platform,  as xamarin:{ android, ios, win},  web api websire + angular, and web form web site old school.. all of this solutions will use the same source to gmailMe  

    Monday, October 29, 2018 5:38 PM
  • User-1312778766 posted

    you(PatriceSc) say: 

    starting by making 100% sure if you are entering this " if (Request.Form.Keys.Count != 4)" block.

    its on the aspx website being call from xamarin... i will try to see if i could return the key number..  1s

    Monday, October 29, 2018 5:54 PM
  • User475983607 posted

    its happened to be many websites working together - multiple solution and multiple platform,  as xamarin:{ android, ios, win},  web api websire + angular, and web form web site old school.. all of this solutions will use the same source to gmailMe  

    You have to understand that the code, the problem description, and your responses simply do not make sense.  It feels like we are missing the context.  The code shown snippets shown are browser based technologies; ASPX and CORS.  Yet you are not using a browser?  You're showing login and logout code through, what looks like, a proxy which is a questionable design.  Lastly, if you possess the skill set to meld several technologies, why are you having such a hard time sending an email?

    Monday, October 29, 2018 5:56 PM
  • User-1312778766 posted

    i get code 200 even if the aspx page throw an exaction..

    its working very well with js ajax but no some much with HttpClient call as i know it.

    from here i will stop and will set a web method or old school generic handler. 

    thanks.

    Monday, October 29, 2018 6:28 PM
  • User753101303 posted

    Or try to just change that to :

     if (Request.Form.Keys.Count != 4)
            {
                Response.StatusCode=400; // Bad request
                return;
            }

    and see if you see now 400 Bad request. For now if this test is entered you won't send the mail but you still get a 200 OK. I believe the problem could be using the "text/html" content type (which is when you get an html page not when you post form fields).

    If you have an exception you should have that (or do you have something special in place when an exception happens ?). Also make sure once again that you do report an incorrect key count one way or the other....

    Monday, October 29, 2018 6:29 PM
  • User475983607 posted

    2xo1

    i get code 200 even if the aspx page throw an exaction..

    its working very well with js ajax but no some much with HttpClient call as i know it.

    Did you change the content-type to "application/x-www-form-urlencoded" as suggested above? 

    Even so, the way the code is written it will always return a 200 OK.  Try setting a breakpoint and single stepping through your code.

    I recommend that you build a Web API endpoint to handler email rather than an ASPX page.

    Monday, October 29, 2018 6:35 PM
  • User-1312778766 posted

    2xo1

    its happened to be many websites working together - multiple solution and multiple platform,  as xamarin:{ android, ios, win},  web api websire + angular, and web form web site old school.. all of this solutions will use the same source to gmailMe  

    You have to understand that the code, the problem description, and your responses simply do not make sense.  It feels like we are missing the context.  The code shown snippets shown are browser based technologies; ASPX and CORS.  Yet you are not using a browser?  You're showing login and logout code through, what looks like, a proxy which is a questionable design.  Lastly, if you possess the skill set to meld several technologies, why are you having such a hard time sending an email?

    the logout was from xamarin to web api+angular,

    the HttpClient call was to a  aspx page website from xamarin..

    usually this page use only js ajax call (not from any of the above solutions), but this time i thought not to send the email from the device as trying not to add more dlls ref and try to make the mobile application lighter..

    thank you,

    i am ending here.

    Monday, October 29, 2018 6:43 PM
  • User61956409 posted

    Hi 2xo1,

    If possible, please clarify more about your scenario and requirement, not just which code is not working, which would help us better understand your question.

    the logout was from xamarin to web api+angular,

    the HttpClient call was to a  aspx page website from xamarin..

    Would you like to make same website login user logout the system when the user logout from xamarin client?

    With Regards,

    Fei Han

    Tuesday, October 30, 2018 7:36 AM