21 Ekim 2016 Cuma

c# ile php arasında iletisim

.
C#—-> Send Data —-> PHP Page —-> Processing Data —-> C# Get Information.
Let me Show you the code
Usage Example , C# Code:
1
2
3
4
5
6
7
8
9
10
using System;
 
class Program
{
    static void Main(string[] args)
    {
        string Data = Web.GetPost("http://www.smart-arab.com/example.php", "UserName", "Me", "Password", "123");
        Console.WriteLine(Data);
    }
}
PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--?PHP
  
if (isset($_POST['UserName'])) {
    $UserName = $_POST['UserName'];
} else {
    $UserName = null;
}
  
if (isset($_POST['Password'])) {
    $Password = $_POST['Password'];
} else {
    $Password = null;
}
  
  
echo $UserName . " Is And Password Is " . $Password
  
?-->
Please remove the comment from the PHP code before using it.
C# Class : ( You don’t need to go through it deeply, just add it to your Project as a Class file )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
 
class Web
{
    public static 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;
    }
}
Happy Posting :)