Solved

Web service works when run from URL, but not when called in another webservice

Posted on 2014-07-28
1
207 Views
Last Modified: 2014-07-30
Hi.

I have written a web service to that passes parameters to another web service.  When pass the parameters via the URL, the data is passed successfully, but when I am passing it via my C# web service, I get an error.  Code below:

Uri address = new Uri("http://10.20.30.40:8841/webservice/test1.php");

                        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

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

                        StringBuilder data = new StringBuilder();
                        data.Append("source=" + HttpUtility.UrlEncode(strCompany));
                        data.Append("&user=" + HttpUtility.UrlEncode(strusername));
                        data.Append("&password=" + HttpUtility.UrlEncode(strpassword));
                        data.Append("&phone_code=" + HttpUtility.UrlEncode(strPhoneCode));
                        data.Append("&phone_number=" + HttpUtility.UrlEncode(strCellNumber));
                        data.Append("&first_name=" + HttpUtility.UrlEncode(strFirstName));
                        data.Append("&last_name=" + HttpUtility.UrlEncode(strLastName));

                        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
                        request.ContentLength = byteData.Length;
                        using (Stream postStream = request.GetRequestStream())
                        {
                            postStream.Write(byteData, 0, byteData.Length);
                        }

Open in new window


The error that I get says :System.Net.WebException: Unable to connect to the remote server
But when I put the URL in my browser, it works.  Am I missing something?
0
Comment
Question by:Jasmin01
1 Comment
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
Comment Utility
I noticed that below code should have @

Uri address = new Uri(@"http://10.20.30.40:8841/webservice/test1.php");


Additionally, it may expect the credentials to access. One of below code blocks could be used.


request.Credentials = CredentialCache.DefaultCredentials

Open in new window

or

request.Credentials = new NetworkCredential(xxx,xxx);
request.PreAuthenticate = true;

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now