2012-08-09 72 views
0

URL页面的更新来更新一个网页(如果你点击它,它只是一个样本连接将无法正常工作):https://test-services.zzz111.org/yyy-9/center/api/rest/v1/pols/ZYPK做一个HTTP POST使用C#

我假设我需要做POST在这个URL上。

这里有一个请求的样本:

<PolChangeSet schemaVersion="2.1" username="[email protected]" description="Adding a note"> 
    <Attachment name="pic.jpg" contentType="image/jpeg"> 
     <Description/> 
     <Location>https://services.zzz111.com/yyy-9/center/api/sdo/rest/v1/buckets/attachments/objects/6BD0C43B-4608-0EDE-F6DA-919097EFCABF.jpg</Location> 
    </Attachment> 
</PolChangeSet> 

我怎么会去发送该HTTP POST请求的网址是什么?

+0

更详细地解释一下,你究竟做了什么?什么标题和内容你想发布到该网址等...。 – saber 2012-08-09 22:55:09

回答

1

如果您需要发布在后端,你可以参考这个http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx

而且通过xml传递URI和Paramaters作为字符串

我编辑了原始源并将contentType添加为附加参数。 XML类型为"application/xml"

public static string HttpPost(string URI, string Parameters, string contentType) 
{ 
    System.Net.WebRequest req = System.Net.WebRequest.Create(URI); 
    req.Proxy = new System.Net.WebProxy(ProxyString, true); 
    //Add these, as we're doing a POST 
    req.ContentType = contentType; 
    req.Method = "POST"; 
    //We need to count how many bytes we're sending. Post'ed Faked Forms 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()); 
    return sr.ReadToEnd().Trim(); 
} 
+0

感谢杰森,我打算给这个试试:) – Testifier 2012-08-10 13:39:56

+0

为什么我需要一个代理呢? – Testifier 2012-08-10 13:42:41

+0

不需要代理,你可以放心地把它拿出来。从演示中,包含它,这样您可以根据需要添加代理服务器 – 2012-08-13 00:57:49

0

Here is example。 尝试一下,如果遇到任何困难,请在此处发帖。现在这个问题看起来像“为我写代码”

+0

我已经阅读过文章,并且已经有了使用POST上传文件的代码。但我不确定如何将xml /文本添加到POST正文。 – Testifier 2012-08-09 22:48:49

+0

将request.ContentType更改为“text/xml”,并使您的示例请求实际为xml(添加)。 – dantix 2012-08-09 22:50:32