我正在尝试使用ONLY url参数创建一个简单的HTTP帖子。

至少我理解以下说明:

  

使用名为url的单个参数POST到该地址,该参数是已更改的Feed的地址。

     

与XML-RPC方法一样,它会验证Feed已更改,如果是,则会通知订阅者。

     

记录事件。返回值是一条名为result的XML消息,带有两个属性,success和msg。

这是我目前的代码:

        public static void ping(string feed)
    {
        HttpWebResponse response = MakeRequest(feed);
        XmlDocument document = new XmlDocument();

        document.Load(response.GetResponseStream();
        string success = document.GetElementById("success").InnerText;
        string msg = document.GetElementById("msg").InnerText;

        MessageBox.Show(msg, success);
    }
        private static HttpWebResponse MakeRequest( string postArgument)
    {
        string url = path + "?" + UrlEncode(postArgument);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        return (HttpWebResponse)request.GetResponse();
    }
    private static string UrlEncode( string value)
    {
        string result;
        result= HttpUtility.UrlEncode("url") + "=" + HttpUtility.UrlEncode(value);
        return result;
    }

我收到来自服务器的错误响应,所以我假设我在某种程度上做错了。这是回复:

  

在文档的顶层无效。处理资源'file:/// C:/Users/ADMIN/AppData/Local/Temp/VSD1.tmp.XML ...

时出错      

芸香   ^

任何想法??

提前致谢

有帮助吗?

解决方案 3

以下是我发现的代码。

我们使用HTTP POST,在流体中编码参数。这给了我们“url-foobar”。作为内容体。身体中没有内容类型,处置,边界等。

        private static HttpWebResponse MakeRequest(string path, string postArgument)
    {
        //string url = path + "?" + UrlEncode(postArgument);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);

        request.Method = "POST";
        string boundary = Guid.NewGuid().ToString().Replace("-", "");
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        Stream stream = request.GetRequestStream();
        string result = string.Format("url={0}", postArgument);
        byte[] value = Encoding.UTF8.GetBytes(result);
        stream.Write(value, 0, value.Length);
        stream.Close();

        return (HttpWebResponse)request.GetResponse();
    }
        public static void ping(string server, string feed)
    {
        HttpWebResponse response = MakeRequest(server, feed);
        XmlDocument document = new XmlDocument();
        string result = GetString(response.GetResponseStream());
        try
        {
            document.LoadXml(result);
        }
        catch
        {
            MessageBox.Show("There was an error with the response", "Error");
        }
        //MessageBox.Show(msg, success);

    }
    public static string GetString(Stream thestream)
    {
        int n = thestream.ReadByte();
        byte[] bytes = new byte[n];
        thestream.Read(bytes, 0, n);
        return Encoding.ASCII.GetString(bytes);
    }

对GetString的调用仅用于调试目的,并不是严格需要的。

感谢所有在这里欢呼的人让我走上正轨。

其他提示

我不知道.NET的API,但是:我理解指令为:“在带有查询参数的URL上执行帖子”,帖子正文应该有url = foobar参数。

IOW:不是采用postArgument并将其附加到url,而是应该调用url并在邮件正文中提供正确编码的url = foobar。

此外:我没有看到您设置请求'Accept'标头,如果服务器使用该标头来识别要响应的格式,这可能很重要。

在.NET中执行表单发布的最佳方法是使用WebClient类。该类将执行正确的事情来发送数据,作为POST(具有实体主体中的参数)或作为GET(具有编码为查询字符串参数的参数)。

你需要向我们展示客户回馈的实际异常,以便弄清楚出了什么问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top