Solved

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

Posted on 2014-07-28
1
224 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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…

733 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