我做了下面的代码来调用一个web服务数据输出,它工作了。问题是当我激活web软件上的api密钥并生成这个密钥时,调用web服务并需要使一个api客户端通过一个httpRequest请求它,但是每当我运行它时,我的问题是“远程服务器返回了一个错误:(404)找不到”。你知不知道?我把我的全部代码都贴上了。
提前谢谢你
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DisplayDataInformation
{
public partial class DisplayDataInformation : Form
{
public DisplayDataInformation()
{
InitializeComponent();
}
private void Submit(object sender, EventArgs e)
{
localhost.Dashboard proxy = new localhost.Dashboard();
localhost.ProjectMetaData[] pm = proxy.GetAllProjectMetaData();
const string URL = "http://localhost/myProgram/";
const string apiKey = "d26b15b5-e336-48de-9142-939c0e639e8f";
const string Id = "Id";
const string Pass = "pass";
System.Net.HttpWebRequest myHttpWReq;
System.Net.HttpWebResponse myHttpWResp;
//myHttpWReq.ContentLength = 0;
// Make a web request to the web service
myHttpWReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(BLUEURL + "http://localhost/myProgram/Dashboard.asmx" + Id + Pass + apiKey);
myHttpWReq.Method = "Get";
// Get the response of the web service
myHttpWResp = (System.Net.HttpWebResponse)myHttpWReq.GetResponse();
if (myHttpWResp.StatusCode == System.Net.HttpStatusCode.OK)
{
//Create an XML reader to parse the response
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(myHttpWResp.GetResponseStream());
}
//set an string output to the windows form
StringBuilder sb = new StringBuilder();
foreach (localhost.ProjectMetaData value in pm)
{
sb.AppendLine(value.ProjectTitle + " - "
+ value.ProjectID + " - "
+ value.PublishStatus );
// sb.AppendLine("\r\n\t");
}
label1.Text = sb.ToString();
}
}
}
发布于 2014-09-18 11:54:56
看起来,您最像是错误地生成请求URL。您已经创建了这样的:
myHttpWReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(BLUEURL + "http://localhost/myProgram/Dashboard.asmx" + Id + Pass + apiKey);
此代码生成的URL为:
http://localhost/myProgram/Dashboard.asmxIdPassd26b15b5-e336-48de-9142-939c0e639e8f
(注意:我在您的代码示例中找不到变量BLUEURL的值,但它将作为您生成的URL的前缀,不管它是什么。我最好的猜测是,它是空白的,因为否则您将得到一个UriFormatException。
这看起来不像您想要的正确的URL,因此出现404错误。你可能想要的是:
myHttpWReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(String.Format("http://localhost/myProgram/Dashboard.asmx?id={0}&pass={1}&apiKey={2}", Id, Pass, apiKey));
这将将请求发送到URL
http://localhost/myProgram/Dashboard.asmx
在查询字符串中发送参数。
然而,这可能仍然会给您一个错误,因为您试图调用一个asmx服务,这是一个基于XML的web服务,因此您的请求格式错误。
本教程将向您展示如何将Dashboard.asmx web服务作为服务引用添加到项目中。这将自动生成一个代理类,它将所有对web服务的调用包装成简单的函数调用。
有关如何添加参考的更多帮助,请阅读以下答案:
https://stackoverflow.com/questions/25875632
复制相似问题