Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

HttpWebRequest.GetRequestStream() is not working in CRM Plugin

Posted on by Microsoft Employee

Hi all,

I have written a plugin wherein I am trying to get an XML response. 

This is my code :

// Set the Method property of the request to POST.
string strXMLServer = "xxx";
var request = (HttpWebRequest)WebRequest.Create(strXMLServer);
request.Method = "POST";
// Set the ContentType property of the WebRequest. request.ContentType = "xyz"; // Assuming XML is stored in strXML byte[] byteArray = Encoding.UTF8.GetBytes(strXML);
// Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length;
//(LINE 5) Get the request stream
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream. 
dataStream.Write(byteArray, 0, byteArray.Length);

// Close the Stream object.
dataStream.Close();

This code works fine when its written in a console application. But when I copy the same code to a class library(plugin) and tries to debug it using plugin profiler, the application gets stopped abruptly when it reaches (LINE 5)

i.e. At Stream dataStream = request.GetRequestStream();

 

NOTE: I am using Dynamics 365 online trial version

Any help would be appreciated :)
Thanks in advance






 


*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HttpWebRequest.GetRequestStream() is not working in CRM Plugin

    Hi Mehatin, i have the same issue.  Kindly did same thing like u did, still i have the issue, my tool stops working.

    My  Code

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

             request.Method = "POST";

             request.ContentType = "application/x-www-form-urlencoded";

             request.Headers.Add("Authorization: " + "Bearer" + " " + KEY);

             request.ContentLength = data.Length;

             Stream stream = request.GetRequestStream();

             {

                 stream.Write(data, 0, data.Length);

             }

             var response = (HttpWebResponse)request.GetResponse();     //error tool crashes, i took off the debugger also.

             var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

             }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HttpWebRequest.GetRequestStream() is not working in CRM Plugin

    Hi! I know a plugin that can help log calls automatically in MS Dynamics. That app is Tenfold, a CTI and sales acceleration platform that offers features like call logging and call monitoring. You can check out Tenfold on the integrations page if you'd like to know more. www.tenfold.com/.../dynamics

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HttpWebRequest.GetRequestStream() is not working in CRM Plugin

    Hi samhitha,

    Thanks for the response. The debugger is not supported in LINE5 and it gets stopped abruptly. I have changed the breakpoint after the GetRequestStream() function and it worked. I found that many people are facing the same issue. Hope this issue gets noticed and solved.

  • Verified answer
    Samhitha Nittala Profile Picture
    Samhitha Nittala 75 on at
    RE: HttpWebRequest.GetRequestStream() is not working in CRM Plugin

    Hi Meharin Sherif

    I faced the same issue while calling the web API methods using plugin.

    I used the same code u did for Dynamics 365 online trial, and GetRequestStream() worked for me.

    Make sure you set content.length=0, when the strXML is null. it will be better if you can return the response from a function or use tracing service to log it.

    Please don't put the debugger on the Line 5 when using profiler as it is getting crashed for me too. Skip that line and you can check the returned response.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HttpWebRequest.GetRequestStream() is not working in CRM Plugin

    I did something like this with JSON. You can use xml serializer instead of JSON. I am using classes for request and response objects here.

    string streamData = "";

               using (MemoryStream stream = new MemoryStream())

               {

                   DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ResponseClass));

                   serializer.WriteObject(stream, responseObject);

                   stream.Position = 0;

                   using (StreamReader reader = new StreamReader(stream))

                   {

                       streamData = reader.ReadToEnd();

                   }

               }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HttpWebRequest.GetRequestStream() is not working in CRM Plugin

    Hello Mohd Saad Akhtar,

    Thanks for your response.

    But this link also uses request.GetRequestStream() function for XML request. The issue is, when it reaches this function plugin gets stopped abruptly. It works fine with console application.  Any idea why this happens?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HttpWebRequest.GetRequestStream() is not working in CRM Plugin

    Refer this:

    www.greenbeacon.com/.../invoke-external-web-service-from-ms-crm-2011-plugin

    https://softnovation.wordpress.com/2016/08/02/consume-web-service-from-plugin/

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,104 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans