【问题标题】:Windows phone 8.1 HttpWebRequestWindows 手机 8.1 HttpWebRequest
【发布时间】:2015-11-30 13:54:56
【问题描述】:

我在我的 Windows 手机应用程序中实现 HttpWebRequest 时遇到了一些问题。

这是我第一次研究这个类,我不知道如何实现方法:

这是我的要求:

            HttpWebRequest request = WebRequest.Create(fullPath) as HttpWebRequest;

我的目标是返回 2 个值:一个 ref 字符串,它是下载文件的名称(它与传递给 URL 的文件名不同,因为标题是我的 sha256 代码。)和内容的 JsonTextReader下载的文件。

这就是我需要总结的方法:

shaDownloaded = response.Headers["Content-Disposition"].Replace("attachment; filename=", String.Empty).Replace("\"", String.Empty);
reader = new StreamReader(response.GetResponseStream());
JsonTextReader jReader = new JsonTextReader(reader);
return jReader;

我的问题是我不知道如何达到这一点。我尝试了一些方法,但我总是让下载与 stackTrace 异步,所以总是空响应。任何人都可以帮助实施一个好的方法吗?这是我的部分代码:

public JsonTextReader DownloadFileFromService(string fileUrl, string fileName, string oldSha, ref string newSha)
    {
        try
        {
            string fullPath = string.Format(fileUrl + fileName + "&sha=" + oldSha);
            StreamReader reader;

            HttpWebRequest request = WebRequest.Create(fullPath) as HttpWebRequest;

            // Get response here {

                shaDownloaded = response.Headers["Content-Disposition"].Replace("attachment; filename=", String.Empty).Replace("\"", String.Empty);

                reader = new StreamReader(response.GetResponseStream());
            //}

            JsonTextReader jReader = new JsonTextReader(reader);
            return jReader;

        } catch(Exception ex){
            return null;
        }
    }

我无法真正解决这个问题。感谢您提供任何帮助的建议

【问题讨论】:

    标签: c# windows-phone-8.1 httpwebrequest


    【解决方案1】:

    嗯,我不知道我是否理解你的问题,但我试着回答

    这就是我使用HttpWebRequest的方式,尝试实现它。

    public async Task<object []> ApiCommand(string api, string json)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(Variables.apiURL + api);
            httpWebRequest.ContentType = "text/plain; charset=utf-8";
            httpWebRequest.Method = "POST";
    
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
            try
            {
                HttpResponseMessage response = await client.PostAsync(Variables.apiURL + api, new StringContent(json, Encoding.UTF8, "application/json"));
    
                return new[] {response.StatusCode.ToString(), await response.Content.ReadAsStringAsync()};
            }
            catch (Exception ex)
            {
                return new[] {"EXCEPTION", ex.ToString()};
            }
        }
    

    【讨论】:

    • 您好,感谢您的回答。我可能解释得很糟糕..我唯一的问题是httpclient,它没有在windows phone上实现:(
    • 之所以实现是因为我在我的项目中使用它只是添加了dll
    • 好的,我必须执行很多,但效果很好!非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多