c#做桌面与php对接,如何将C#桌面应用程序中的xml发送到php服务器脚本并进行解析?...

在我的项目中,我想编写一个桌面应用程序,该应用程序以文件或字符串的形式发送xml文档,

到需要解析它的服务器上的php脚本.

现在,我有解析xml的php脚本,并已在桌面应用程序中准备好了xml.

我的问题是:

一种.哪种方法更好:将文档作为文件或字符串发送?

b.如何通过C#实现请求(文件或字符串),以及如何通过php接受文档.

笔记:

一种.我只能在桌面应用程序上使用C#.

b.我只能在服务器上使用php脚本.

C.我使用System.xml.linq在桌面应用程序中处理xml文档.

非常感谢!

解决方法:

您可以根据自己的喜好简单或复杂;-)

这是一个非常简单的基本示例(没有错误处理等):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Linq;

using System.Net;

namespace LinqXMLTest

{

class Program

{

static void Main(string[] args)

{

Object[] res = { "Stackoverflow", true, 'x', 42};

XElement xml = new XElement("Foo",

from a in res select

new XElement("bar",

new XElement("type", a.GetType()),

new XElement("value", a.ToString())

)

);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");

request.Method = "POST";

xml.Save( request.GetRequestStream() );

HttpWebResponse resp = request.GetResponse() as HttpWebResponse;

}

}

}

并作为“接收者”:

$fp = fopen('php://input', 'rb');

file_put_contents('test.dat', $fp);

之后服务器上的文件test.dat包含

System.String

Stackoverflow

System.Boolean

True

System.Char

x

System.Int32

42

标签:desktop-application,xml,c,net,php

来源: https://codeday.me/bug/20191208/2088730.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 C# 桌面程序示例,可以每隔 5 秒钟获取串口中的地磅实时重量数据,并将其发送到指定的网站。 ```csharp using System; using System.IO.Ports; using System.Net; using System.Text; using System.Threading; namespace WeightSender { class Program { static void Main(string[] args) { // 初始化串口 var port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); port.Open(); // 启动定时器 var timer = new Timer(SendWeight, null, TimeSpan.Zero, TimeSpan.FromSeconds(5)); // 等待用户输入任意键退出程序 Console.WriteLine("Press any key to exit."); Console.ReadKey(); // 停止定时器和串口 timer.Dispose(); port.Close(); } static void SendWeight(object state) { // 读取串口数据 var weight = ReadWeight(); // 发送数据到网站 var url = "https://example.com/weight"; var data = $"weight={weight}"; var bytes = Encoding.UTF8.GetBytes(data); var request = WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = bytes.Length; using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } var response = request.GetResponse(); response.Close(); // 输出调试信息 Console.WriteLine($"Sent weight: {weight}"); } static string ReadWeight() { // TODO: 从串口中读取地磅实时重量数据,并返回字符串格式的重量值 // 这里只是一个示例,实际的串口通信需要根据具体的硬件设备和通讯协议来实现 return "123.45"; } } } ``` 需要注意的是,这个示例程序只是一个框架,需要根据实际情况来实现具体的串口通信和网站数据发送逻辑。同时,为了防止数据发送失败导致程序异常退出,建议在发送数据时加上异常处理代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值