我正在使用 C# Windows 应用程序。我有一个 php 文件,它使用 javascript/ajax 来验证登录并返回一些数据。当我在浏览器中执行此操作时,它工作正常,但是当我通过 httpwebrequest 从 c# windows 应用程序发送数据时,它不会返回完整的响应。 c#提交浏览器数据后没有得到响应。
请帮我解决这个问题。
下面是我的代码
<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;
}
最佳答案
尝试添加header()手动指定响应的函数
关于javascript - 如何使用 httpwebrequest 在 C# 中获取网页(使用 ajax/javascript 的 php)的最终响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29429142/