【发布时间】:2018-04-17 22:26:39
【问题描述】:
我用 C# 编写了一个简单的 WCF Restful Web 服务,以针对某些参数获取一些值。这是我的 IService 代码:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
string GetURL(string pVr);
这是 Service.svc 的代码
public string GetURL(string pVr)
{
try
{
string VersionCode = ConfigurationManager.AppSettings["MobAppVersionCode"];
if (Convert.ToInt32(VersionCode) > Convert.ToInt32(pVr))
{
return "http://xxx.xxx.xxx.xxx/abc.apk";
}
else
{
return "No apk available";
}
}
catch (Exception ex)
{
throw ex;
}
}
我的目的之一是当我将此 Web 服务发布到 IIS 时,该方法应该被隐藏(不再需要休息或帮助页面或 wsdl),以便我更改 Web 配置并像这样设置
<behaviors>
<serviceBehaviors>
<behavior name="SEILServiceBehaviour">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" httpHelpPageEnabled="false" httpsHelpPageEnabled="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="false"/>
</behavior>
</endpointBehaviors>
</behaviors>
现在,在将此 Web 服务发布到 IIS 后,我可以通过邮递员或 Web 浏览器轻松查看服务结果,甚至可以通过 Retrofit2 android 库获取数据。但是当我用我的 HttpUrlConnection 代码连接这个 Web 服务时,它会显示错误 405-Method Not Allowed。这是我的连接页面的代码
public void onRequest(String urlParameters) {
try {
//Establishing connection with particular url
URL url = new URL("http://xxx.xxx.xxx.xxx/Service.svc/GetURL");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//Setting connection timeout
connection.setReadTimeout(30000);
connection.setConnectTimeout(30000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
InputStream inputStream;
int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK)
inputStream = connection.getErrorStream();
else
inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder response = new StringBuilder();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
if (url.getHost().equals(connection.getURL().getHost())) {
urlReceive = response.toString().trim();
} else {
urlReceive = "Internet Login Required";
}
} catch (IOException e) {
urlReceive = "Connection Timed Out";
}
}
请帮帮我
【问题讨论】:
-
我在邮递员中使用了 GET。并得到了完美的结果。在android改造中我也得到了完美的结果(不需要使用任何GET或POST)
-
是的,我们不能在 android 代码中使用 get,因为我要针对 web 服务发布一些参数,以便我可以获得一些价值。但我也尝试在 android 端使用 get 。它会给我错误 GET not allowed here。
-
是的,当我在 post man 中点击这个 api 时,它会得到错误 405-method not allowed。这意味着我也必须从移动端进行 GET 调用。但不知道该怎么做
-
你终于完成了这项工作,先生。只需将 android 请求方法从 POST 更改为 GET,删除 setDoOutput 行并将 urlParameters 与 URL 和“?”连接起来中途,得到了完美的结果
标签: c# android rest web-services wcf