Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 34
VB.NET (2008) - Refactoring Question 2 23
VS2013 publish C# application Created folder -- where is it??? 10 27
itextsharp with c# 3 5
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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