web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

HttpWebRequest.GetRequestStream() is not working in CRM Plugin

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Community Member Profile Picture
    on at

    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/

  • Community Member Profile Picture
    on at

    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?

  • Suggested answer
    Community Member Profile Picture
    on at

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

                   }

               }

  • Verified answer
    Samhitha Nittala Profile Picture
    75 on at

    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.

  • Verified answer
    Community Member Profile Picture
    on at

    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.

  • Community Member Profile Picture
    on at

    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

  • Community Member Profile Picture
    on at

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

             }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans