Solved

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

Posted on 2014-07-28
1
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 40227441
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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