Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 11 23
Graphics 2 28
Expression Evaluater 3 24
VB.NET 2008 - SQL Timeout 9 24
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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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