【问题标题】:Send long uri to rest service with query part使用查询部分发送长 uri 到休息服务
【发布时间】:2015-02-19 11:27:06
【问题描述】:

这是我的场景: 我有一个 Rest 服务,在查询部分有一个字符串参数,它将被反序列化为一个包含 byte[] 的复杂对象:

        [OperationContract]
        [WebInvoke(Method = "PUT",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, 
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json?doc={doc}")]
        string UploadDocument(string doc);

这是我的 UploadDocument 继承:

public string UploadDocument(string doc) 
{
    if(string.IsNullOrEmpty(doc))
        return "ko";
    JavaScriptSerializer jscript = new JavaScriptSerializer();
    Document inputDocument = jscript.Deserialize<Document>(doc);
    return "ok";
}

我需要从读取文件内容的客户端调用它,并将其放入参数中:

JavaScriptSerializer jscript = new JavaScriptSerializer();
            Document newComp = jscript.Deserialize<Document>(test);
            newComp.Content = File.ReadAllBytes(@"D:\Test\PdfParse\test.pdf");
            newComp.filename = "test.pdf";

            test = jscript.Serialize(newComp);
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] data = encoder.GetBytes(test);
            WebRequest request = WebRequest.Create("http://localhost/DocumentUpload/DocumentUpload.svc/json?doc=");
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = data.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();
            WebResponse wr = request.GetResponse();

            using (StreamReader sw = new StreamReader(wr.GetResponseStream()))
            {
                test = (sw.ReadToEnd());

            }

问题 如果我在 WebRequest.Create 中添加“测试”内容,则 url 太长,但将其写入 requestStream 休息服务返回给我,“doc”始终为空。 可以通过什么方式传递参数?

【问题讨论】:

    标签: c# wcf rest webrequest


    【解决方案1】:

    您可以在您的方法中使用 multipart_form_data 媒体类型。 在这种情况下,您可以直接将流传递给您的代码。

    我认为这个链接会帮助你: http://www.briangrinstead.com/blog/multipart-form-post-in-c

    祝你好运

    【讨论】:

      猜你喜欢
      • 2010-12-24
      • 2015-12-22
      • 2016-06-30
      • 2018-07-08
      • 2017-03-22
      • 2018-08-30
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      相关资源
      最近更新 更多