如何使用httpwebrequest在c#中获得网页(使用ajax/javascript的php)的最终响应


How to get final response of webpage (php that uses ajax/javascript) in c# using httpwebrequest

我使用的是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()函数以手动指定响应