使用邮递员,我可以执行获取并发送Auth-Token
作为标题密钥,然后使用Z4TTHmY98=gFw2rG
这样的值,并且它会进入web api方法,因为它会根据自定义属性进行检查。< / p>
但是,我想使用浏览器
这是被击中的api
http://localhost:29001/api/test
My Header Key = Auth-Token
我的标头值= Z4TTHmY98 = gFw2rG
在阅读了一些文章后,我认为这样可行,但是在逐步调试web api方法时标题为空。
可以用浏览器吗?如果是这样,怎么样?
http://localhost:29001/api/test?Auth-Token=Z4TTHmY98=gFw2rG
答案 0 :(得分:1)
浏览器打开页面不会发送自定义标头。但您可以使用您需要的自定义headeres发送AJAX请求。以下示例使用jQuery.ajax
ajaxSetup
<强>更新强>
如果您想通过页面向每个AJAX请求发送相同的标头,可以使用beforeSend
+ $.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('Auth-Token', 'Z4TTHmY98=gFw2rG');
}
});
:
public static void main(String[] args){
double tolerance = 1E-9, referenceArea =0.0, exactArea =0.0, x =0.0, y =0.0, error = 0.0, approximateArea = 0.0;
int totalPointsCounter =0, hitPointsCounter =0;
char firstCharacter ;
String titleText, messageText;
titleText = "The Monte Carlo Method";
int userInput = JOptionPane.showConfirmDialog(null, "Run a Monte Carlo Experiment?", titleText, JOptionPane.YES_NO_OPTION);
if (userInput == 1){
JOptionPane.showMessageDialog(null, "The program terminates \nGood Bye!", titleText, JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
String userExperimentInput = JOptionPane.showInputDialog(null, "Please Enter\n1 for Experiment 1\n2 for Experiment 2\n3 for Experiment 3\n4 for Experiment 4", titleText, JOptionPane.QUESTION_MESSAGE).trim();
if(userExperimentInput == null || userExperimentInput.equals("") ){
JOptionPane.showMessageDialog(null, "No input received\nThe program terminates", titleText, JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
char theCase = userExperimentInput.charAt(0);
switch (theCase){
case '1':
exactArea = Math.pow(2, -0.5)*1/2;
referenceArea = 1.0;
boolean condition = error < tolerance;
double percentHit = (((double)hitPointsCounter/(double)totalPointsCounter));
approximateArea =(percentHit)*(referenceArea);
error = exactArea - Math.abs(approximateArea);
int counter = 0;
do {
x= Math.random();
y= Math.random();
totalPointsCounter++;
if (y==0 && x==0){hitPointsCounter++;}
if (y>0){
if ((x/y)>=Math.pow(2, 0.5)){
hitPointsCounter++;}
}
exactArea = Math.pow(2, -0.5)*1/2;
referenceArea = 1.0;
counter++;
}
while(condition);
percentHit = (((double)hitPointsCounter/(double)totalPointsCounter));
approximateArea =(percentHit)*(referenceArea);
error = exactArea - Math.abs(approximateArea);
messageText ="Experiment #1" + ": \n\nMC needed " + totalPointsCounter + " random points for tolerance " + tolerance + "\nThe approximate area is " + approximateArea;
JOptionPane.showMessageDialog(null, messageText, titleText, JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
exactArea = Math.PI;
referenceArea = 4.0;
case 3:
exactArea = (1.0/3.0);
referenceArea = 2.0;
case 4:
exactArea = 2;
referenceArea = Math.PI;
default:
System.out.println("Wrong character for case number, program terminates");
System.exit(0);
}
}
答案 1 :(得分:0)
使用Webapi 2参数绑定。
您可以从浏览器拨打电话,如下所示
http://localhost:29001/api/test/Z4TTHmY98=gFw2rG/
[HttpGet]
[Route("/test/{AuthToken}")
public IHttpActionResult Test([FromUri] string AuthToken)
{
//Do whatever you want to do.
}
小心路线。它需要正确并在webapiconfig
中配置。
答案 2 :(得分:0)
按页面排列的AJAX请求可以使用ajaxSetup
+ beforeSend
:
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('Auth-Token', 'Z4TTHmY98=gFw2rG');
}
});
<强>信息强>
如果您想使用相同的方式调用第三方API,则可以使用
objHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Key", "value");
objHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "zMXGa8WNQuaBxWazgUhO2dw=");