简体   繁体   中英

Windows phone 8.1 HttpWebRequest

I have some problems implementing HttpWebRequest in my Windows phone app.

It's the first time I work on this class and I don't get how to implement methods:

This is my request:

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

My aim is to return 2 values: a ref string wich is the name of the file downloaded(It is NOT same as the file name passed to URL, because the title is my sha256 code.) and the JsonTextReader of the content of file downloaded.

This is how I need to conclude the method:

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;

My problem is that I don't know how to reach this point. I tryed some ways but i always had the download going async with the stackTrace, so always null response. Anyone can help implementing a good Method? this is my portion of code:

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;
        }
    }

I can't really solve this. Thanks in advice for any help

Hmm I don't know If I understood what's your problem but I try to answer

It's how I use HttpWebRequest, try to implement it.

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()};
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM