0

我已经使用字符串发布数据从服务器发布和获取,但现在我有了下一个 WebService:

submitFeedback(String token, String subject, String body, byte[] photo, byte[] video);

private void PostFeedbackData()
    {
        if (GS.online != true)
        {
            MessageBox.Show(GS.translations["ErrorMsg0"]);
        }
        else
        {
            HttpWebRequest request = HttpWebRequest.CreateHttp("https://****/feedback");
            request.Method = "POST";
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
        }
    }
    void GetRequestStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
        // End the stream request operation
        Stream postStream = myRequest.EndGetRequestStream(callbackResult);

        // Create the post data
        string postData = "";

        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            postData = "{\"jsonrpc\": \"2.0\", \"method\": \"getUserSchedule\", \"params\":[" + "\"" + (App.Current as App).UserToken + "\",\"" + FeedbackTitle.Text + "\",\"" + FeedbackContent.Text + "\",\"" + imageBytes + "\"], \"id\": 1}";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Add the post data to the web request
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();

            // Start the web request
            myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
        });

    }

查看发布数据 - 它是一个字符串,我不能将 imageBytes 数组放在那里。怎么做?

4

1 回答 1

0

有什么理由使用 HttpWebRequest 吗?

我之所以这么问,是因为我认为将文件发送到服务的正确方法是通过多部分数据。使用 HttpWebRequest 你必须手动实现它。

这是一个使用 Microsoft 的 HttpClient 的示例,它可能对您有用:

public async Task<string> submitFeedback(String token, String subject, String body, byte[] photo, byte[] video) {
    var client = new HttpClient();
    var content = new MultipartFormDataContent(); 

    // Some APIs do not support quotes in boundary field
    foreach (var param in content.Headers.ContentType.Parameters.Where(param => param.Name.Equals("boundary")))
        param.Value = param.Value.Replace("\"", String.Empty);

    var tok = new StringContent(token);
    content.Add(tok, "\"token\"");
    var sub = new StringContent(subject);
    content.Add(tok, "\"subject\"");

    // Add the Photo 
    var photoContent = new StreamContent(new MemoryStream(photo));
    photoContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
        Name = "\"photo\"",
        FileName = "\"photoname.jpg\""
    };
    photoContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
    content.Add(photoContent);

    // Add the video
    var videoContent = new StreamContent(new MemoryStream(video));
    videoContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
        Name = "\"video\"",
        FileName = "\"videoname.jpg\""
    };
    videoContent.Headers.ContentType = MediaTypeHeaderValue.Parse("video/mp4");
    content.Add(videoContent);

    HttpResponseMessage resp;

    try {           
        resp = await client.PostAsync("SERVER_URL_GOES_HERE", content);
    }
    catch (Exception e)
    {
        return "EXCEPTION ERROR";
    }

    if (resp.StatusCode != HttpStatusCode.OK)
    {
        return resp.StatusCode.ToString();
    }

    var reponse = await resp.Content.ReadAsStringAsync();

    return reponse;
}

相应地改变。注意:HttpClient 也在后台使用 HttpWebRequest。此外,我认为让 UI 线程发出请求并不是一个好主意。这是没有意义的,因为您阻塞了接口并使异步理论变得无用。

于 2013-08-09T17:45:40.847 回答