1
我正在使用c#windows应用程序。我有一个使用javascript/ajax验证登录并返回一些数据的php文件。当我在浏览器中执行它时,它工作正常,但是当我通过httpwebrequest从c#windows应用程序发送数据时,它不会返回完整的响应。提交浏览器数据后,c#没有得到响应。如何在c#中使用httpwebrequest获取网页(使用ajax/javascript的php)的最终响应
请帮助我。
下面是我的代码
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
function Ajaxcall(){
$.ajax({
url: "XXXXXXXX", // this url is a moodle url and validate user authentication then returns roleid
type: "POST",
async:true,
data: {username: uname, password: pwd, TransactionId: TransactionId},
error: function (data)
{
document.write("Error");
},
success: function(response)
{
var url = 'login.php';
var form = $('<form action="' + url + '" method="post">' +
'<input type="text" name="RoleId" value="' + response + '" />'
'<input type="text" name="TransactionId" value="' + TransactionId + '" />'
'</form>');
$('body').append(form);
form.submit();
}
});
}
<?php
//The below code will automatically execute after form submission
if (isset($_POST["RoleId"]))
{
$NewRoleId = $_POST["RoleId"];
$TransactionId = $_POST["TransactionId"];
$qry = "Update query is to update roleid in a table based on the transactionid";
$result = mysql_query($qry);
if ($result)
echo "Updated successfully";
else
echo "Error in updating query";
exit();
}
'The below code runs when the page is loading and gets trnsactionid from the 'c#
if (isset($_POST['TransactionId']))
{
$qry = "select query returns username, password";
echo "query:". $qry;
$result = mysql_query($qry);
if (mysql_num_rows($result) > 0)
{
$rows = mysql_fetch_array($result);
echo "<script type='text/javascript'>var uname = '" . $rows["username"] . "', pwd = '" . $rows["password"] . "', TransactionId='" . $_POST['TransactionId'] . "';</script>";
echo "<script language='JavaScript'>Ajaxcall(callbackfunction); </script>";
}
}
?>
C#代码发布数据:
String response = GetPost("XXXXX/Login.php", "TransactionId", "1")
public string GetPost(string Url, params string[] postdata)
{
string result = string.Empty;
string data = string.Empty;
System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
if (postdata.Length % 2 != 0)
{
MessageBox.Show("Parameters must be even , \"user\" , \"value\" , ... etc", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return string.Empty;
}
for (int i = 0; i < postdata.Length; i += 2)
{
data += string.Format("&{0}={1}", postdata[i], postdata[i + 1]);
}
data = data.Remove(0, 1);
byte[] bytesarr = ascii.GetBytes(data);
try
{
WebRequest request = WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytesarr.Length;
System.IO.Stream streamwriter = request.GetRequestStream();
streamwriter.Write(bytesarr, 0, bytesarr.Length);
streamwriter.Close();
WebResponse response = request.GetResponse();
streamwriter = response.GetResponseStream();
System.IO.StreamReader streamread = new System.IO.StreamReader(streamwriter);
result = streamread.ReadToEnd();
streamread.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return result;
}
感谢响应。你能指定我必须添加标题()和需要写在标题中的什么地方。谢谢 – Sahana 2015-04-03 10:53:22
如果我正确理解你,你在UPDATE查询后没有回应。因此,在exit()之前添加状态和内容类型的头文件; 您是否检查了从PHP代码中的C#发送的POST请求的内容? – 2015-04-03 11:17:06
从c#发送数据后,我在php文件中获得“TransactionId”。这里没有问题。但是if(isset($ _ POST [“RoleId”])){}中写入的代码没有达到。 ajaxcall函数没有调用,或者当我从c#windows应用程序调用时,不会发生相同的页面重定向。请告诉我如何解决这个问题。 – Sahana 2015-04-03 11:37:25