如何创建看起来像这样的请求

时间:2016-08-15 11:54:15

标签: c# http httpwebrequest

我需要使用C#和HttpWebRequest复制HTTP请求,如下所示:

  

HEADER:POST / upnp / control / x_contact HTTP / 1.1主机:fritz.box:49000   连接:Keep-Alive User-Agent:...

到目前为止,除了POST之后的部分外,我得到了一切正常的工作

" / upnp / control / x_contact HTTP / 1.1 "。

我如何修改HttpWebRequest以将这些额外数据提供给服务器?

PS:这是java代码的外观,它会生成正确的请求。我想将此代码移植到C#并从那里发送相同的请求:

socket = new Socket(InetAddress.getByName("fritz.box"), 49000);

// Send header
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));
writer.write("POST /igdupnp/control/WANIPConn1 HTTP/1.1");
writer.write("Host: fritz.box:49000\r\n");
writer.write("SOAPACTION: \"urn:schemas-upnp-org:service:WANIPConnection:1#GetStatusInfo\"\r\n");
writer.write("Content-Type: text/xml; charset=\"utf-8\""+"\r\n");
writer.write("Content-Length: " + myData.length() + "\r\n");
writer.write("\r\n");

// Send data
writer.write(myData);

writer.flush();

到目前为止,这是我的C#代码(产生不完整的HTML请求):

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create (@"http://fritz.box:49000");

webRequest.Headers.Add ("SOAPACTION", "urn:schemas-upnp-org:service:WANIPConnection:1#GetStatusInfo");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";

0 个答案:

没有答案