Call webservice on an external server from javascript using asp.net and c #

I am trying to test web service calls using an ASP.NET page that creates a form with username and password fields and a submit button. (Both jQuery and the .js file that I use are included in the script tags in the head element.)

The Submit button calls a function created in C # code behind the file, which invokes a separate JavaScript file.

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);
}

The JavaScript function Authenticatemakes a web service call using jQuery and Ajax to another server, sending JSON parameters and waiting for a JSON response in response.

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",
        url: 'https://myHost.com/V1/Identity/Authenticate',
        data: params,
        contentType: 'application/json'
    });

    alert(xmlhttp.statusText);
    alert(xmlhttp.responseText);

    return;
}

However, since the web service I am calling is on a different server than the ASP.NET, C #, and JavaScript files, I am not getting a warning statusTextor responseText.

- -, , . beforeSend, . - ?

!

jjnguy, Janie Nathan, - HttpWebRequest. jjnguy, , .

public static void Authenticate(string pwd, string uname)
{
    string ret = null;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://myhost.com/V1/Identity/Authenticate");
    request.ContentType = "application/json";
    request.Method = "POST";

    string data = "{\"Header\":{\"AuthToken\":null,\"ProductID\":\"NOR\",\"SessToken\":null,\"Version\":1},\"ReturnAuthentication\":true,\"Password\":\"" + pwd + "\",\"Username\":\"" + uname + "\",\"ReturnCredentials\":false }'";

    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
    request.ContentLength = byteData.Length;

    using (Stream postStream = request.GetRequestStream()) 
    {
        postStream.Write(byteData, 0, byteData.Length);
    }

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    using (response)
    {
        // Get the response stream  
        StreamReader reader = new StreamReader(response.GetResponseStream());

        // Console application output  
        ret = reader.ReadToEnd();
    }

    Console.WriteLine(ret);
}

, (400) Bad Request , HttpWebRequest. Response {System.Net.HttpWebResponse}, Status - ProtocolError. , , URL- HTTP SSL. , , , URL- ASP.NET HTTPS ( )?

+5
4

, , , , , .

    string data = "{\"Header\":{\"AuthToken\":null,\"ProductID\":\"NOR\",\"SessToken\":null,\"Version\":1},\"ReturnAuthentication\":true,\"Password\":\"" + pwd + "\",\"Username\":\"" + uname + "\",\"ReturnCredentials\":true}";
+3

, - # ?

#, Javascript.

#:

public static string Authenticate(string pwd, string uname)
{
    HttpWebRequest requestFile = (HttpWebRequest)WebRequest.Create("https://myHost.com/V1/Identity/Authenticate");
    requestFile.ContentType = "application/json";
    requestFile.Method = "POST";
    StreamWriter postBody = new StreamWriter(requestFile.GetRequestStream())
    using (postBody) {
        postBody.Write("{\"Header\":{\"AuthToken\":null,\"ProductID\":\"NOR\",\"SessToken\":null,\"Version\":1},\"ReturnAuthentication\":true,\"Password\":\"" + pwd + "\",\"Username\":\"" + uname + "\",\"ReturnCredentials\":false }'");
    }
    HttpWebResponse serverResponse = (HttpWebResponse)requestFile.GetResponse();
    if (HttpStatusCode.OK != serverResponse.StatusCode)
        throw new Exception("Url request failed.  Connection to the server inturrupted");
    StreamReader responseStream = new StreamReader(serverResponse.GetResponseStream());
    string ret = null;
    using (responseStream) {
        ret = responseStream.ReadLine();
    }
    return ret;
}

.

+2

, script, ; ,

EDIT, :

- - , script: ( , https://myHost.com/V1/Identity/Authenticate)

, # js ( , .)

, , ; - , .

, :

Visual Studio - , WebClient HttpRequest

WebClient: http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx

HttpWebRequest: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(VS.80).aspx

+1

,

http://en.wikipedia.org/wiki/Same_origin_policy

I think there are ways around this, but I think the other posters are right. On the server, write methods that use HttpWebRequest to call the web service, and then use the JavaScriptSerializer to parse JSON. I spent most of the day exploring this reason, and I will have to write something like this to myself.

>>>>  Nathan

PS I like @Janie to plan better ... Can you do this using a web service that returns JSON as well as one that will transmit XML?

0
source

All Articles