【问题标题】:HTTP POST and complex structuresHTTP POST 和复杂的结构
【发布时间】:2009-11-17 12:22:13
【问题描述】:

我正在尝试向 Web 服务发送一个复杂的 HTTP POST 请求。 Web 服务是使用 VS2008 创建的,您可以在其中设置 VS 以创建 HTTP POST 和 GET 接口以及 SOAP 接口。

现在请求要求提供字符串参数(用户名、文件描述等)和文件本身,表示为 Base64Binary。

在 VS 中创建 Web 服务(不是 asp.net Web 服务)并将它们设置为接受 HTTP POST 请求时 - 是否可以发送包含字符串参数和二进制数据的 HTTP POST 请求?

【问题讨论】:

    标签: .net soap http-post


    【解决方案1】:

    您可以使用Convert.ToBase64String() 方法获取您的字节并转换为base64 字符串。所以,你会得到:

    string base64 = Convert.ToBase64String(File.ReadAllBytes("yourfile.ext"));
    

    如果你在谈论如何发送它,你可以使用HttpWebRequest,像这样:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("service.asmx");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    using (Stream post = request.GetRequestStream())
    {
        string querystring =  // note you must encode that values
                    "name=" + HttpUtility.UrlEncode(name) +
                   "&desc=" + HttpUtility.UrlEncode(description) +
                   "&data=" + HttpUtility.UrlEncode(base64);
        byte[] data = Encoding.UTF8.GetBytes(querystring);
        post.Write(data, 0, data.Length);
    }
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
    

    【讨论】:

    • 我尝试了您的代码,但仍然收到服务器错误 (500)。我重新表述了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多