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) {
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)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
ret = reader.ReadToEnd();
}
Console.WriteLine(ret);
}
, (400) Bad Request , HttpWebRequest. Response {System.Net.HttpWebResponse}, Status - ProtocolError. , , URL- HTTP SSL. , , , URL- ASP.NET HTTPS ( )?