0

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

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

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

4

1 回答 1

0

您可以使用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());
于 2009-11-17T12:28:19.777 回答