1

I've the following postman request: enter image description here enter image description here

Which returns me as expected an URL: enter image description here

I'm trying to mimic this operation with a .Net Core 2.0 application, with the following code:

static void Main(string[] args)
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://epaper.20minuten.ch/index.cfm/epaper/1.0/getEditionDoc");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";

    var serializer = new Newtonsoft.Json.JsonSerializer();
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        using (var tw = new Newtonsoft.Json.JsonTextWriter(streamWriter))
        {
            serializer.Serialize(tw,
                new
                {
                    editions = new[]
                    {
                            new
                            {
                                defId = "648",
                                publicationDate = "2018-03-06"
                            }
                    },
                    isAttachment = true,
                    fileName = "Gesamtausgabe_Lausanne_2018-03-06.pdf"
                });
        }
    }
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();
        Console.ReadLine();
    }
}

But I receive 500 errors. What did I miss?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • You are using a lot of methods that aren't really ideal here. Most of the time you want to use HttpClient to make Restful requests. Also, try using `JsonConvert.SerializeObject()`, its typically what I use and what most code examples use. – maccettura Mar 06 '18 at 16:26
  • 2
    `isAttachment` is misspelled. I would run Fiddler, and compare the payload of the good request and the bad request – Jonathan Mar 06 '18 at 16:27
  • @maccettura I'm open to suggestions, how would you do this? – J4N Mar 06 '18 at 16:29
  • There is nothing wrong with HttpWebRequest, particularly in a console app that cannot run async code without blocking. – Crowcoder Mar 06 '18 at 16:29
  • @J4N [this answer](https://stackoverflow.com/a/15176685/2457029) shows how to post with HttpClient. Although you should not use a `using` statement with HttpClient, see [here](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). For JsonConvert, see the [documentation](https://www.newtonsoft.com/json/help/html/SerializingJSON.htm) – maccettura Mar 06 '18 at 16:31
  • @Crowcoder I know there is nothing _wrong_ with it. I just figured I would point OP to some simpler/more modern APIs. A lot of (IMO) needlessly complex code here, the OP might have an easier time debugging with the more straightforward APIs – maccettura Mar 06 '18 at 16:33
  • @maccettura, I agree HttpWebRequest is more complex to use but a kitten dies everytime a developer writes `Wait()` or `.Result`. . – Crowcoder Mar 06 '18 at 16:36
  • @Crowcoder Agree on the deadlocking/sync issues. As for HttpClient being reused I already addressed that in my comment (I said dont use a `using` statement and I linked to the infamous blog article describing why its a bad idea) – maccettura Mar 06 '18 at 16:37
  • 1
    @maccettura my bad, I removed that from my comment. – Crowcoder Mar 06 '18 at 16:38
  • @Jonathan I'm sorry, I have already corrected this but I still have the issue. I tried to get fiddler, but I'm quite new in this tools and I'm not sure what to look for. – J4N Mar 06 '18 at 16:40
  • 1
    If you are interested in using RestSharp you can have Postman generate that c# code for you by clicking the "Code" link and choosing `C# (RestSharp)` from the dropdown. – Crowcoder Mar 06 '18 at 16:43
  • With fiddler running, make a call with Postman, and a call with your app. Short version: click on each of the requests in the list on the left, inspector in the top right. The top right pane is the request, bottom right is the response. Click through json, raw, etc tabs to see the request and response formatted in different ways. – Jonathan Mar 06 '18 at 16:44
  • @Crowcoder I'm open to anything :) Didn't know this feature of Postman, and absolutely love it! It work like a charm now! Not sure why! Can you please add this comment as an answer? – J4N Mar 06 '18 at 16:47

2 Answers2

3

Postman can generate a wide variety of code. To use RestSharp with C# click the code button/link and choose C# (RestSharp) from the dropdown.

It should resemble:

enter image description here

Someday I will contribute a generator for HttpClient and HttpWebRequest

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
0

I guess the problem might be due to the fact that you are calling a external url there might be cases where they block crawling requests like these. Try setting a useragent for the request to mimic a browser call.

Liju L
  • 124
  • 1
  • 4
  • 1
    I added ` httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36";` but I still got the same issue. And why would it work in postman? – J4N Mar 06 '18 at 16:43
  • postman always set headers like useragents similar to a browser call. Also try catching the postman call using fiddler. – Liju L Mar 06 '18 at 16:47
  • @J4N Just added user agent in the header and worked for me Thanks. – Vaibhav Gole Apr 17 '20 at 09:31