【发布时间】:2015-07-08 20:00:04
【问题描述】:
我希望能够编写与在我的计算机上运行的应用程序交互的代码,该应用程序正在侦听端口 5587。
static void Main(string[] args)
{
HttpPost("http://localhost:5587", "/send/?username=testingname&password=testingpw");
}
public static string HttpPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy = WebRequest.DefaultWebProxy;
//Add these, as we're doing a POST
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//We need to count how many bytes we're sending. Params should be name=value&
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
resp.Close();
return sr.ReadToEnd().Trim();
}
当我阅读应用程序的规格时,它说:
1) POST 请求应该是:
http://HOST:PORT/send/? 用户名=用户& 密码=密码& 到=目的地& 密送=密送目的地& 主题=消息主题
2) 而且 POST data 应该是:
message=messageBody
3) 此外,此 POST 请求返回队列中消息的 UUID。此 UUID 稍后用于检查队列中消息的状态。
我如何更改此代码以完成我需要做的三件事,包括获取带有 ID 的响应,以便我可以将其保存以备后用?
编辑:
我需要的输出与我当前输出的输出的屏幕截图。
【问题讨论】: