c# - 将 C# Web 服务参数传递给 PHP 应用程序

标签 c# php asp.net web-services

我正在尝试将 C# Web 服务参数传递给 PHP 应用程序,但下面是我的代码。实际上我正在传递用户名和密码 xml 格式,因为没有伙伴在传递时不应该看到该凭据。

下面是我的 C# Web 服务,它使用 asp.net Web 表单按钮单击重定向 PHP 应用程序。

  [WebMethod]
public string POSTXml(string username, string password)
{
    WebRequest req = null;
    WebResponse rsp = null;
    try
    {
        StringBuilder strRequest = new StringBuilder();

        string url = "http://xyz.in/getuser.php/";

        req = WebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "text/xml";

        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        writer.WriteLine(username,password);
        writer.Close();

        rsp = req.GetResponse();

        var sr = new StreamReader(rsp.GetResponseStream());
        string responseText = sr.ReadToEnd();

        return responseText;

    }
    catch (Exception e)
    {
        throw new Exception("There was a problem sending the message");
    }
}

下面是我的按钮点击代码。

  protected void Button2_Click(object sender, EventArgs e)
{

    localhost.WebService objserv1 = new localhost.WebService();
    Label.Text = objserv1.POSTXml("nagapavani", "tech@1234");


}

实际上,当用户单击按钮时,将一些值传递给网络服务,并希望通过网络服务将该值传递给 php 应用程序。有没有其他方法可以实现该要求。当我要单击按钮时不会重定向并从谷歌获取此代码。

最佳答案

您可以按如下方式发送数据。转换为字节数组写入请求流:

[WebMethod]
public string POSTXml(string username, string password)
{
    WebRequest req = null;
    WebResponse rsp = null;
    try
    {
        string data = "user=" + username + "&password=" + password;
        string url = "http://xyz.in/getuser.php/";

        byte[] buffer = Encoding.ASCII.GetBytes(data);
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);


        WebReq.Method = "POST";
        WebReq.ContentType = "application/x-www-form-urlencoded";
        WebReq.ContentLength = buffer.Length;

        using (Stream PostData = WebReq.GetRequestStream())
        {
            PostData.Write(buffer, 0, buffer.Length);

            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

            using (Stream stream = WebResp.GetResponseStream())
            {
                using (StreamReader strReader = new StreamReader(stream))
                {
                    return strReader.ReadToEnd();                            
                }                        
            }
            WebResp.Close();
       }

    }
    catch (Exception e)
    {
        throw new Exception("There was a problem sending the message");
    }
    return String.Empty;
}

关于c# - 将 C# Web 服务参数传递给 PHP 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42022660/

相关文章:

c# - 如何在复杂的 simpleDb 实体上实现 CRUD

c# - 来自应用程序的错误 : Could not extract Info. plist:无法将 plist 文件 '/Pathof.appfile/Info.plist' 解析为 XML

ASP.NET HttpApplication生命周期

c# - 大开关的替代品?

c# - 尝试读取或写入 protected 内存。这通常表明其他内存已损坏。使用 OpenFileDialog 时

c# - lambda 表达式

php - jQuery Form Plugin 不按 AJAX 提交表单,它仍在重新加载页面,为什么?

php - 在后台运行 PHP 脚本(不打扰用户)?

php - 无法通过 PHP 修改内容安全策略 header

ASP.NET - SEO 友好吗?