Search code examples
c#.netwebrequest

Make http WebRequest work in C#


I want to get a respond from an http website,I have used this code

// Create a new request to the mentioned URL.    
WebRequest myWebRequest = WebRequest.Create("http://127.0.0.1:8080/geoserver/NosazMohaseb/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=NosazMohaseb:GParcelLAyer&maxFeatures=50&outputFormat=application%2Fjson&bbox=5727579.437775434,3838435.3419322656,5727581.1322169611,3838437.0363737918");

//   var myWebRequest = WebRequest.Create(myUri);
myWebRequest.Method ="GET";
myWebRequest.Timeout = TimeOut;

if (myWebRequest is HttpWebRequest)
{
   ( myWebRequest as HttpWebRequest).Accept = "application/json";
    (myWebRequest as HttpWebRequest).ContentType = "application/json";
    //(myWebRequest as HttpWebRequest).Accept = 
    (myWebRequest as HttpWebRequest).KeepAlive = false;
    (myWebRequest as HttpWebRequest).UserAgent = "SharpMap-WMSLayer";
}

if (Credentials != null)
{
    myWebRequest.Credentials = Credentials;
    myWebRequest.PreAuthenticate = true;
}
else
    myWebRequest.Credentials = CredentialCache.DefaultCredentials;

if (Proxy != null)
    myWebRequest.Proxy = Proxy;

try
{
    using (var myWebResponse = (HttpWebResponse)myWebRequest.GetResponse())
    {           
        using (var dataStream = myWebResponse.GetResponseStream())
        {
            var cLength = (int)myWebResponse.ContentLength;
        }
        myWebResponse.Close();
    }
}
catch (WebException webEx)
{
    if (!this.ContinueOnError)
        throw (new RenderException(
            "There was a problem connecting to the WMS server when rendering layer '" + LayerName + "'",
            webEx));
}
catch (Exception ex)
{
    if (!ContinueOnError)
        throw (new RenderException("There was a problem rendering layer '" + LayerName + "'", ex));
}

But when I try to get cLength it is -1,So it does not work,But When I try to access this website

http://127.0.0.1:8080/geoserver/NosazMohaseb/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=NosazMohaseb:GParcelLAyer&maxFeatures=50&outputFormat=application%2Fjson&bbox=5727579.437775434,3838435.3419322656,5727581.1322169611,3838437.0363737918

I get following answer

{"type":"FeatureCollection","totalFeatures":2,"features":[{"type":"Feature","id":"GParcelLAyer.14970","geometry":{"type":"Polygon","coordinates":[[[5727597.96542913,3838442.73401128],[5727595.60003176,3838429.21114233],[5727576.62444883,3838431.10604568],[5727571.16785106,3838432.76483769],[5727569.78420277,3838437.30665986],[5727570.19434939,3838439.63808217],[5727597.96542913,3838442.73401128]]]},"geometry_name":"geom","properties":{"FK_BlockNo":"12055","FK_LandNo":"8","NoApart":"100000","Name":"                    ","Family":"??","Father":"                    ","MeliNo":"          ","MalekType":"1   ","PostCode":"0         ","Id_Parvande":null,"BuildNo":null,"BuildTypeCode":null,"BuildUserTypeCode":null,"BuildViewTypeCode":null,"BuildGhedmatCode":null,"Farsoode":"0"}}],"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::900913"}}}

So it seems I am missing somthing while getting respond using C#..Can you please help me find my mistake? thanks


Solution

  • In your code you're actually just getting response stream and later you're not reading anything from this stream - that's why you're not getting any data.

    You have to create stream reader and use it to read data from response stream (consider to use buffered Read instead of ReadToEnd if your data size is large):

    using (var dataStream = myWebResponse.GetResponseStream())
    using (var reader = new StreamReader(dataStream))
    {
        string data = reader.ReadToEnd();
    }
    

    Concerning ContentLength equals to -1 in your case - well, it can be something at your server-side, check if your server actually returns this header. In fact, this header is not mandatory and you should not rely on it.