WebService错误DownloadString(字符串地址) [英] WebService Error DownloadString(String address)

查看:113
本文介绍了WebService错误DownloadString(字符串地址)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我迫切需要帮助.我有一个可以在本地计算机上正常运行的Web服务,但是当我将其移植到生产Web服务器时,出现以下错误:

Hi All I''m a little desperate for help. I have a web service that runs fine on my local machine but when I port it over to the production web server I get the following error:

"System.Net.WebException: The remote server returned an error: (500) Internal Server Error. at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadString(Uri address) at System.Net.WebClient.DownloadString(String address) at edChartLink2.edChartLink2.GetToken(String acctNum, String MRN) in C:\Inetpub\wwwroot\edChartLink2\edChartLink2\edChartLink.asmx.cs:line 50 "



代码段为:



Code Snippet is:

48            string tokenURL= "http://emhaallrca/hmaled_emh/GetToken.aspx?system=3";
49            string strToken = "";            
50            strToken = client.DownloadString(tokenURL);



我从Web服务调用的页面都驻留在同一Web服务器"emhaallrca"上

任何帮助将不胜感激.

谢谢
Jose''



the page I''m calling from my web service both reside on the same web server "emhaallrca"

Any help would be greatly appreciated.

Thanks
Jose''

推荐答案

经验法则:
除非没有其他方法,否则永远不要使用计算机名/服务器名作为您的代码.原因很简单(您的情况也很明显),当您从一种环境迁移到另一种环境时,它根本就行不通.

现在,解决您的问题.我们可以做两件事.

1.)如果这是本地(在您的应用程序内)Web服务,仅使用相对URL.像
Rule of thumb:
Never ever use machine name/server name is your code unless there is no other way to do it. Reason is simple (which is also obvious is your case) that it will simply not work when you move from one environment to another.

Now, to resolve your problem. We can do two things.

1.) If this is local (within your application) webservice that simply use a relative url. Something like
string tokenURL = GetApplicationUrl() + "/hmaled_emh/GetToken.aspx?system=3";



其中GetApplicationUrl()应该返回您的应用程序URL.为此写功能应该不难.

2.)如果Web服务托管在其他地方,即您的应用程序之外,而不是



Where GetApplicationUrl() should return your application url. Shouldn''t be difficult to write a fuction for this.

2.) If webservice is hosted somewhere else i.e. outside your application than

string tokenURL = "http://YOURDOMAINNAME/hmaled_emh/GetToken.aspx?system=3";


我确实找到了使用WebRequest的解决方案,如下所示,它的工作原理就像一个魅力:

私有字符串callToGetToken()
{
WebRequest tokenRequest = WebRequest.Create("http://emhaallrca/hmaled_emh/GetToken.aspx");
tokenRequest.Method ="POST";
字符串urlPostData ="system = 3";
byte [] tokenByteArray = Encoding.UTF8.GetBytes(urlPostData);
tokenRequest.ContentType ="application/x-www-form-urlencoded";
流tokenDataStream = tokenRequest.GetRequestStream();
tokenDataStream.Write(tokenByteArray,0,tokenByteArray.Length);
tokenDataStream.Close();
WebResponse tokenResponse = tokenRequest.GetResponse();
tokenDataStream = tokenResponse.GetResponseStream();
StreamReader tokenReader =新的StreamReader(tokenDataStream);
字符串tokenData = tokenReader.ReadToEnd();

返回tokenData;

}

希望这对我花了几天时间的其他人有所帮助.
I did find the solution just use WebRequest as follows and it works like a charm:

private string callToGetToken()
{
WebRequest tokenRequest = WebRequest.Create("http://emhaallrca/hmaled_emh/GetToken.aspx");
tokenRequest.Method = "POST";
string urlPostData = "system=3";
byte[] tokenByteArray = Encoding.UTF8.GetBytes(urlPostData);
tokenRequest.ContentType = "application/x-www-form-urlencoded";
Stream tokenDataStream = tokenRequest.GetRequestStream();
tokenDataStream.Write(tokenByteArray, 0, tokenByteArray.Length);
tokenDataStream.Close();
WebResponse tokenResponse = tokenRequest.GetResponse();
tokenDataStream = tokenResponse.GetResponseStream();
StreamReader tokenReader = new StreamReader(tokenDataStream);
string tokenData = tokenReader.ReadToEnd();

return tokenData;

}

Hope this helps somebody else I spent a few days on this.


这篇关于WebService错误DownloadString(字符串地址)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆