大家好,又见面了,我是你们的朋友全栈君。
C#中使用webservice接口的时候,返给服务器的IP地址是带上了端口号的。但是有时候不能要那个端口(比如用nginx做了转发),就需要在服务端处理一下(处理内容就是后面的代码)。此外,需要在配置文件中web.config中的system.web中添加一些东西:
2.如果没有protocols中的内容的话,有可能post和get请求不能被正确识别(未做过验证,只是在博客园上看见过类似问题)。
代码:
//———————————————————————–
//
// * Copyright (C)
// * version :
// * author : ying83811
// * FileName: OuterPortReflector.cs
// * history : created by ying83811
//
//———————————————————————–
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Description;
namespace Better517Na.KoreaProductInterface.CommMethod
{
///
/// OuterPortReflector类
///
public class OuterPortReflector : SoapExtensionReflector
{
///
/// 重写ReflectMethod
///
public override void ReflectMethod()
{
}
///
/// 重写ReflectDescription
///
public override void ReflectDescription()
{
// 为了说明问题,这里直接把端口号写死了,建议写在配置文件中
string portNum = “8888”;
portNum = “:” + portNum;
ServiceDescription description = ReflectionContext.ServiceDescription;
foreach (Service service in description.Services)
{
foreach (Port port in service.Ports)
{
foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
{
SoapAddressBinding binding = extension as SoapAddressBinding;
if (null != binding)
{
binding.Location = binding.Location.Replace(portNum, string.Empty);
}
else
{
HttpAddressBinding httpBinding = extension as HttpAddressBinding;
if (httpBinding != null)
{
binding.Location = “https://baidu.com”;
httpBinding.Location = httpBinding.Location.Replace(portNum, string.Empty);
}
else
{
binding.Location = “https://baidu.com”;
Soap12AddressBinding soap12Binding = extension as Soap12AddressBinding;
if (soap12Binding != null)
{
soap12Binding.Location = soap12Binding.Location.Replace(portNum, string.Empty);
}
}
}
}
}
}
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/139642.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...