1

I have service url : please check it, It's return json string.

I don't know how to call this url by using C#

Then. By the link, you can see "href" in json data. It's have .png url. I would like to save that image to my local disk.

Question

  1. How to get and read json data from this service

  2. How to save image from url to local disk

OammieR
  • 2,800
  • 5
  • 30
  • 51

3 Answers3

2

I imagine you need the service request from the server and not from the client; if so a straightforward way would be

string json = null;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/export?bbox=9153621.1267%2C-644273.9207%2C13435472.9966%2C3637577.9492&bboxSR=&layers=&layerdefs=&size=800%2C600&imageSR=&format=png&transparent=false&dpi=&time=&layerTimeOptions=&f=pjson");
HttpWebResponse response = null;

try
{
    response = (HttpWebResponse)request.GetResponse();
    var responseStream = response.GetResponseStream();

    if ((responseStream != null) && responseStream.CanRead)
    {
        using (var reader = new System.IO.StreamReader(responseStream))
        {
            json = reader.ReadToEnd();
        }
    }
}
finally
{
    if (response != null)
    {
        response.Close();
    }
}

The image can be obtained the same way.

For Json I recommend JSON.NET.

MarkH
  • 31
  • 2
1

Using Data Contracts and DataContractJsonSerializer:

References Required:

using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Net;

Data Contract

[DataContract]
internal class GISData
{
    [DataMember]
    public string href;

    [DataMember]
    public int width;

    [DataMember]
    public int height;

    [DataMember]
    public GISDataExtent extent;

    [DataMember]
    public string scale;
}

[DataContract]
internal class GISDataExtent
{
    [DataMember]
    public string xmin;

    [DataMember]
    public string ymin;

    [DataMember]
    public string xmax;

    [DataMember]
    public string ymax;

    [DataMember]
    public GISDataExtentSpatialReference spatialReference;

}

[DataContract]
internal class GISDataExtentSpatialReference
{
    [DataMember]
    public string wkid;
}

Putting it all together and downloading the file:

Pull the JSON String from your URL - Deserialize the object and download your image.

WebClient webClient;
webClient = new WebClient();
string json = webClient.DownloadString(@"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/export?bbox=9153621.1267%2C-644273.9207%2C13435472.9966%2C3637577.9492&bboxSR=&layers=&layerdefs=&size=800%2C600&imageSR=&format=png&transparent=false&dpi=&time=&layerTimeOptions=&f=pjson");

MemoryStream stream = new MemoryStream((Encoding.UTF8.GetBytes(json)));
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(GISData));
stream.Position = 0;
GISData data = (GISData)ser.ReadObject(stream);
stream.Close();

webClient = new WebClient();
webClient.DownloadFile(data.href, "C:/" + data.href.Substring(data.href.LastIndexOf("/") + 1)); //Save only the filename and not the entire URL as a name.
cillierscharl
  • 7,043
  • 3
  • 29
  • 47
  • you can do things simpler: `GISData data = (GISData)ser.ReadObject(@"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/export?bbox=9153621.1267%2C-644273.9207%2C13435472.9966%2C3637577.9492&bboxSR=&layers=&layerdefs=&size=800%2C600&imageSR=&format=png&transparent=false&dpi=&time=&layerTimeOptions=&f=pjson");` – the_joric Mar 05 '12 at 09:25
  • also you may omit DataXXX attributes :) – the_joric Mar 05 '12 at 09:26
  • @the_joric , thanks; `ReadObject`'s best overload seems to be looking for a stream though? – cillierscharl Mar 05 '12 at 09:34
1
  1. Add Json.NET package (see for reference How to install JSON.NET using NuGet?)
  2. Use the following code:

    using Newtonsoft.Json;
    
    ......
    
    string url = "...";
    
    var webClient = new WebClient();
    var s = webClient.DownloadString(url);
    dynamic d = JsonConvert.DeserializeObject(s);
    var href = ((string) d.href);
    
    webClient.DownloadFile(href, @"d:\file.png");
    

Note that your png file does not exists :)

You may create your POCO class if you want. But if all you need is just download a file — that may not be needed.

Community
  • 1
  • 1
the_joric
  • 11,986
  • 6
  • 36
  • 57